0

So I am have a mysql database on my local system and connecting to that using PHP. I know there is nothing wrong with the the server (Apache) because I have used it in another calls using just php.

The problem I think is with the "ajax() request

I have only just started using ajax. I followed a tutorial to implement a way to check if a username already exists on the DB asynchronously.

All the code works up until the ajax() request.

I don't get any errors. All that I see on screen is the result of this code:

$("#availability_status").html(' Checking availability...'); //Add a loading image in the span id="availability_status"

I have been searching for a solution all day and it's driving me mad. Thank you in advance!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Register</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
        </script>
        <script type="text/javascript">
            $(document).ready(function() {  //When the dom is ready
                alert("ready!");
                $("#username").change(function() { //if therezs a change in the username textbox
                    var username = $("#username").val();//Get the value in the username textbox
                    if(username.length > 3)
                    {
                        $("#availability_status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...');
                        //Add a loading image in the span id="availability_status"

                        $.ajax({ 
                            type: "POST",
                            url: "http://localhost/ajax_check_username.php",
                            data: {username: username}, 
                            success: function(server_response){

                                    if(server_response == '0')//if ajax_check_username.php return value "0"
                                    {
                                        $("#availability_status").html('<img src="available.png" align="absmiddle"> <font color="Green"> Available </font>  ');
                                        //add this image to the span with id "#availability_status"
                                    }
                                    else  if(server_response == '1')//if it returns "1"
                                    {
                                        $("#availability_status").html('<img src="not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
                                    }


                            }

                        });

                    }
                    else
                    {

                    $("#availability_status").html('<font color="#cc0000">Username too short</font>');
                    //if in case the username is less than or equal 3 characters only
                    }
                    return false;
                });
            });
    </script>
    </head>
    <body>
    <div id="content">
    <strong>Register</strong>
      <form action="http://localhost/register2a.php" method="get">
        <div class="style_form">
          <label for="username">Username :</label>
          <input type="text" name="username" id="username" class="form_element"/>
          <span id="availability_status"></span> </div>
        <div class="style_form">
          <label for="full_name">Full Name :</label>
          <input type="text" name="full_name" id="full_name" class="form_element"/>
        </div>
        <div class="style_form">
          <label for="email">Email  :</label>
          <input type="text" name="email" id="email" class="form_element"/>
        </div>
        <div class="style_form">
          <input name="submit" type="submit" value="submit" id="submit_btn" />
        </div>
      </form>
     </div>
    </body>
    </html>

////here is the php file

<?php
$conn = mysqli_connect('localhost', 'root', 'sherly743759', 'login');
//Include the Database connection file
if(isset($_POST['username']))//If a username has been submitted
{
    //check username not taken
    $conn = mysqli_connect('localhost', 'root', 'sherly743759', 'login');

    $username = mysql_real_escape_string($username);
    $query = "SELECT username FROM member WHERE username = '$username';";

    $result = mysql_query($query);

if(mysql_num_rows($result) > 0)
{
echo '1'; //Not Available
}
else
{
echo '0';  // Username is available
}

}

?>
4

3 回答 3

0

我总是喜欢在 AJAX 问题上使用 GET 请求,这意味着使用 type:"GET" 执行 jQuery AJAX 并在 PHP 上使用 GET 读取参数。所以我可以通过在浏览器中运行它来轻松测试 PHP 页面本身这样

localhost/ajax_check_username.php?username=something

于 2013-08-20T01:15:53.737 回答
0

您没有error在 Ajax 调用中处理该案例,只是success- 因此,如果 Ajax 调用失败,则不会重置“检查可用性”状态。

副手,您的 PHP 至少部分失败是因为您正在使用 (但从未设置) $username。您还要打开数据库两次,但这可能是无害的。

<?php
  if(isset($_POST['username']))//If a username has been submitted
  {
    //check username not taken
    $conn = mysqli_connect('localhost', 'root', 'sherly743759', 'login');

    $username = mysql_real_escape_string($_POST['username']);
    $query = "SELECT username FROM member WHERE username = '$username';";

    $result = mysql_query($query);

    if(mysql_num_rows($result) > 0)
    {
      echo '1'; //Not Available
    }
    else
    {
      echo '0';  // Username is available
    }
  }

而且您应该停止关闭?>PHP 文件,这样您就不会意外地回显额外的数据并搞砸您的预期结果。

于 2013-08-20T01:18:22.910 回答
0

需要检查您的 jquery 版本。它使用旧版本 1.3... 不兼容

http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
于 2013-09-20T02:30:42.313 回答