0

我想#username_input根据该 php 脚本返回的值显示。似乎 if 条件不起作用。

JavaScript:

$(document).ready(function () {
        $('#username_input').keyup(function () {
            $.post('header/check_username.php', {
                    username: form.username.value
                },
                function (result_username) {
                    if (result_username == 'a') {

                        $('#feedback_username').html('choose a username').show();
                    }
                    if (result_username == 'b') {
                        $('#feedback_username').html('Too short').show();
                    }

                });
        }); 

HTML:

<form id="registeration_form" name="form" onsubmit="return hello()" >
    <table>
        <tr>
            <td>Username </td>
            <td><input type="text" id="username_input" name="username" autofucus/></td>
            <td><span id="feedback_username"></span></td>
            <br/><br/>
        <tr>
            <td>Password </td>
            <td><input type="password" id="password_input" name="password"/></td>
        </tr>
        <br/><br/>
        <tr>
            <td>Confirm Password</td>
            <td><input type="password" id="password_confirm" name="password_confirm"/></td>
            <td><span id="feedback_password"></span></td>
        </tr>
        <br/><br/>
        <tr>
            <td>Email </td>
            <td><input type="text" id= "email_input" name="email" /></td>
        </tr>
    </table>
    <center><input type="submit" x value="Register"/></center>
    <br/>
    <div id= "onsubmit_feedback"></div>
    <center><a href="javascript:register('hide');">close</a></center>
</form>

a如果用户名为空且b用户名少于 6 个字符,则返回。

PHP:

<?php
$con = mysqli_connect("localhost:3306", "root", "", "project_new");
if (mysqli_connect_errno($con)) {

    echo "Unsuccessful" . mysqli_connect_error();
    exit();
}

@$username      = mysqli_real_escape_string($con, trim($_POST['username']));
$check          = mysqli_query($con, "select u from users where u ='$username'");
$check_num_rows = mysqli_num_rows($check);

if ($username == NULL)
    retutn 'a';
    //echo "choose a username";
else if (strlen($username) <= 5)
    return 'b';
    //echo "Too Short. Minimum 6 characters";
else {
    if ($check_num_rows == 0)
        echo "Available :)";
    else if ($check_num_rows == 1)
        echo "Not Available :( ";

}

?>
4

2 回答 2

0

我在以下两行中发现了两个问题(@Fred 在评论中已经提到了一个):

if ($username==NULL)
    retutn 'a';

首先,你$username永远不会为空。 mysqli_real_escape_string返回字符串。第二行retutn 'a'可能是 Fred 建议的错字。

尝试将这些行更改为:

if (empty($username))
    echo 'a';
于 2013-07-27T06:52:47.560 回答
0

您的问题在于错误的回调函数和 PHP 字符串。在 jQuery 的 API 文档之后,成功的回调函数获取三个参数(data、textStatus、jqXHR)。

试试这个 PHP 文件的解决方案:

<?php
$con = mysqli_connect("localhost:3306", "root", "", "project_new");
if (mysqli_connect_errno($con)) {

    echo "Unsuccessful" . mysqli_connect_error();
    exit();
}

$username      = mysqli_real_escape_string($con, trim($_POST['username']));
$check          = mysqli_query($con, "select u from users where u ='$username'");
$check_num_rows = mysqli_num_rows($check);

if (empty($username)) {
    echo 'a';
    //echo "choose a username";
}
else if (strlen($username) <= 5) {
    return 'b';
    //echo "Too Short. Minimum 6 characters";
}
else {
    if ($check_num_rows == 0)
        echo "Available :)";
    else if ($check_num_rows == 1)
        echo "Not Available :( ";

}

?>

这适用于 JavaScript:

$(document).ready(function () {
    $('#username_input').keyup(function () {
        $.post('header/check_username.php', {
                username: form.username.value
            },
            function (data, textStatus, jqXHR) {
                if (data == 'a') {

                    $('#feedback_username').html('choose a username').show();
                }
                if (data == 'b') {
                    $('#feedback_username').html('Too short').show();
                }

            }
        );
    });
});
于 2013-07-27T21:56:56.193 回答