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 和 account_creation 一样存储在 ../customers/ 中。

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

任何帮助将一如既往地受到赞赏。

问候莫迪斯

4

3 回答 3

1

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

url:"../customers/ajax_check_login.php?wantedusername="+wantedusername,
success:function(data){...
于 2013-07-15T22:03:50.897 回答
1

第二个脚本标签应该是

<script type="text/javascript">

不是

<script type="text/javascript" src="../js/jquery-1.7.1.min.js">
于 2013-07-15T21:59:23.210 回答
1

您需要src="../js/jquery-1.7.1.min.js"从包含您的 JavaScript 代码的脚本标记中删除 - 该src属性仅在引用外部文件时有效。如果您想知道,这是 W3C 的解释(重点是我的):

脚本可以在 SCRIPT 元素的内容中定义,也可以在外部文件中定义。如果没有设置 src 属性,用户代理必须将元素的内容解释为脚本。如果 src 有一个 URI 值,用户代理必须忽略元素的内容并通过 URI 检索脚本。

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

于 2013-07-15T22:01:05.117 回答