根据php的返回值提交jquery表单

时间:2013-07-27 06:41:58

标签: php jquery html mysql forms

我想根据php脚本返回的值在#username_input中显示。似乎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;如果用户名少于6个字符,则返回b

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 :( ";

}

?>

2 个答案:

答案 0 :(得分:0)

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

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

首先,您的$username永远不会为空。 mysqli_real_escape_string返回字符串。第二行retutn 'a'可能是弗雷德建议的拼写错误。

尝试将这些行更改为:

if (empty($username))
    echo 'a';

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

            }
        );
    });
});