Jquery Ajax用户名验证无效

时间:2013-07-15 21:51:55

标签: jquery ajax

我在这里看了多个线程试图解决我的难题,但我无法理解它。对于初学者;我试图修改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一样

我想让它做的是验证用户名是否在页面上可用。注意:当我单击我的提交按钮时,它会转到../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

3 个答案:

答案 0 :(得分:1)

第二个脚本标记应为

<script type="text/javascript">

不是

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

答案 1 :(得分: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

答案 2 :(得分:1)

我认为您可能无法正确传递数据,请尝试

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