2013-07-15 60 views
0

我已經看過多個主題,試圖解決我的難題,但我無法理解它。對於初學者;我已經嘗試過嬰兒牀並修改了http://phpseason.wordpress.com/2013/02/17/live-username-availability-checking-using-ajax-and-jquery/,我將js1.7.1保存在../js/jquery-1.7.1.min.js中,我的ajax_check_login存儲在../customers/中,就像account_creation一樣。Jquery Ajax用戶名驗證不起作用

我試圖讓它做的是驗證,如果用戶名可在網頁上直播。注意:當我點擊我的提交按鈕時,它會將我帶到../customers/ajax_check_login.php,並正確顯示1或0。

我也在我的調試嘗試中註釋了各種各樣的位。

我對我的主帳戶創建頁面有以下代碼;

<?php 
session_start(); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Account Creation</title> 
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script> 
     <!-- <script type="text/javascript" src="js/jquery-ui-1.8.17.custom.min.js"></script> --> 

     <script type="text/javascript" src="../js/jquery-1.7.1.min.js"> 

     $(document).ready(function(){ 
      $("#wantedusername").change(function(){ 
/*     $("#message").html("<img src='ajax-loader.gif' /> checking...");*/ 
           $("#message").html("checking..."); 

      var wantedusername=$("#wantedusername").val(); 

       $.ajax({ 
        type:"POST", 
        url:"../customers/ajax_check_login.php", 
        data:"wantedusername="+wantedusername, 
         success:function(data){ 
         if(data==0){ 
          /* $("#message").html("<img src='tick.png' /> Username available");*/ 
          $("#message").html(" Username available"); 
         } 
         else{ 
          /* $("#message").html("<img src='cross.png' /> Username already taken"); */ 
          $("#message").html("Username already taken"); 
         } 
        } 
       }); 

      }); 

     }); 

     </script> 
<?php include "../header.php" ; ?></head> 

<body> 

<header class="headerbg"> 




<section class="headertop"> 
<?php include '../nav.php'?> 
</section> 

</header><!--header and menu wrapper end--> 
<align ="center"> 
Thank you for showing interest in registering for a Crosstrend Analysis ltd account. Please note that these require administrator authorisation before the account will become active, this can take up to 72 hours. 

VERSION CONTROL 1.3 

<div align="center"> 
<table width="300" border="0" align="Center" cellpadding="0" cellspacing="1" bgcolor="="#cccccc"> 
<tr> 
<form id="acctcreation" name="acctcreation" method="post" action="../customers/ajax_check_login.php"> 
<td> 
<table width = "100%" border ="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> 
<tr> 
<td colspan="3"><p><Strong> New Account Request </strong></p></td> 
</tr> 
<tr> 
<td width = "78">Username</td> 
<td widith = "6">:</td> 
<td width = "294"><input name="wantedusername" type="text" id="wantedusername"></td> 
<td id="message"></td> 
</tr> 
<tr> 
<td>Password</td> 
<td>:</td> 
<td><input name="wantedpassword" type="password" id="wantedpassword"></td> 
</tr> 
<tr> 
<td>Password Verification</td> 
<td> : </td> 
<td><input name ="verificationpassword" type="password" id ="verificationpassword"></td> 
</tr> 
<tr> 
<td>Email address</td> 
<td>:</td> 
<td><input name="emailaddress" type="text" id="emailaddress"></td> 
</tr> 
<tr> 
<td>Company</td> 
<td>:</td> 
<td><input name="company" type="text" id="company"></td> 
</tr> 

<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td><input type="submit" name="Submit" value="Submit for authentication"> </td> 
</tr> 
</table> 
</td> 
</form> 
</tr> 
</table> 

</div> 

而且我ajax_check_login.php是

<?php 
include "../connect.php"; 

/* print "<script type=\"text/javascript\">"; 
print "alert('I have been called')"; 
print "</script>"; */ 


if(isset($_POST['wantedusername'])){ 
    $wantedusername = mysqli_real_escape_string($mysqli, $_POST['wantedusername']); 

$usernamecheckstring = "SELECT 1 FROM table WHERE username='".$wantedusername."'"; 

$usernamecheckquery = mysqli_query($mysqli, $usernamecheckstring); 

$rows =mysqli_num_rows($usernamecheckquery); 
echo $rows; 
} 
?> 

任何幫助,將不勝感激一如既往。

問候 Maudise

+1

您需要刪除'SRC =」 ../ JS/jQuery的1.7。 1.min.js「'來自包含JavaScript代碼的腳本標記 - 」src'屬性僅在引用外部文件時有效。 –

+0

添加爲答案,解決它,我已經添加了該行,並刪除了好幾次,它似乎沒有幫助。我花了15分鐘寫在這裏,它的工作原理!謝謝 – Maudise

回答

1

第二個腳本標籤應該讀

<script type="text/javascript"> 

<script type="text/javascript" src="../js/jquery-1.7.1.min.js"> 
1

你需要從包含JavaScript代碼的腳本標籤取下src="../js/jquery-1.7.1.min.js" - 的src屬性僅在引用外部文件時有效。如果你想知道,這裏有來自W3C(重點煤礦)的解釋:

腳本可以在SCRIPT元素或 的內容,內外部文件中定義。如果未設置src屬性,則用戶代理必須將元素的內容解釋爲腳本。 如果src的URI值爲 ,則用戶代理必須忽略該元素的內容並通過URI檢索該腳本的 。

參考:http://www.w3.org/TR/html401/interact/scripts.html

1

我想你可能無法正確地傳遞數據,嘗試

url:"../customers/ajax_check_login.php?wantedusername="+wantedusername, 
success:function(data){...