如何从PHP回显中接收数据以使用AJAX显示?

时间:2011-05-23 02:59:36

标签: php javascript jquery html ajax

简单地说:我如何从使用AJAX显示的PHP回显中接收数据?

我有能力使用JQuery;但是,我不知道这是否适合这项工作。我只是在HTML中有一个用户可能输入信息的表单,但是当他们输入数据时,我想检查数据库的可用性。如果输入检查并且没有问题,我不希望显示任何文本,但如果有问题,那么我想显示错误。我目前有一个通过数据库使用PHP和MySQL的注册表单,但是如果没有页面刷新,我就无法得到错误。有没有办法在没有刷新的情况下显示错误?

HTML:

<form action="index.php" method="POST">
            <table>
                <tr>
                    <td colspan="3"><h1>New to the site?</h1></td>
                </tr>
                <tr>
                    <td><span class="login_text">Username:</span></td>
                    <td><input type="text" class="inputs" name="signup_username" value="<?php echo $username ?>"></td>
                </tr>
                <tr>
                    <td><span class="login_text">Password:</span></td>
                    <td><input type="password" class="inputs" name="signup_password"></td>
                </tr>
                <tr>
                    <td><span class="login_text">Password <span style="color: #666666;">(repeat)</span>:</span></td>
                    <td><input type="password" class="inputs" name="signup_passwordrepeat"></td>
                </tr>
                <tr>
                    <td><span class="login_text">Email:</span></td>
                    <td><input type="text" class="inputs" name="signup_email" value="<?php echo $email ?>"></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" id="signupbutton" class="subutton" style="font-family: Georgia, sans-serif;  font-size: 1.5em;  width: 265px;  height: 3em;" value="Sign Up!" name="signup_button"></td>
                </tr>
            </table>
        </form>

PHP:

<?php
            $submit = $_POST["signup_button"];
            $username = mysql_real_escape_string($_POST["signup_username"]);
            $password = mysql_real_escape_string($_POST["signup_password"]);
            $repeatpassword = mysql_real_escape_string($_POST["signup_passwordrepeat"]);
            $email = mysql_real_escape_string($_POST["signup_email"]);
            $date = date(m.d.y);

            if($submit) {  //If the form is submitted
                if($username && $password && $repeatpassword && $email) {  //If everything is filled out
                    //Check length of username
                    if(strlen($username) <= 16) {
                        $usercheck = mysql_query("SELECT * FROM artists WHERE username='$username'");
                        $usernum = mysql_num_rows($usercheck);

                        if($usernum == 0) {  //Check for availability of username
                            $emailcheck = mysql_query("SELECT * FROM artists WHERE email='$email'");
                            $emailnum = mysql_num_rows($emailcheck);

                            if($emailnum == 0) {  //Check for availability of email address
                                if($password == $repeatpassword) {  //Check for identical password input
                                    //Encrypt password
                                    $password = sha1($password);
                                    $repeatpassword = sha1($repeatpassword);

                                    //REGISTER
                                    $register = mysql_query("INSERT INTO artists VALUES ('', '', '$username', '$password', '$email', '', '$date')");
                                    echo("Registration successful, please log in on the right!");
                                }
                                else
                                    echo("The passwords don't match!");
                            }
                            else
                                echo("That email address has already been registered!");
                        }
                        else
                            echo("That username has already been registered!");
                    }
                    else
                        echo("The username you entered is too long!");
                }
                else
                    echo("Please fill out all fields!");
            }
        ?>

2 个答案:

答案 0 :(得分:2)

jQuery validation plugin是jQuery的标准验证系统,并为基于AJAX的验证提供支持(它将事物发送回服务器以验证它并在失败时为您显示消息)。

答案 1 :(得分:2)

将你想要回复的信息放在json中,例如

<?php echo json_encode(array('message_you_want' => 'your message here'));?>

然后在你的ajax调用json中设置dataType,例如

dataType : 'json'

然后在你的ajax的成功函数中,你可以通过例如

传递json对象
$.ajax({
 dataType: 'json',
 url : 'url',
 data : {/*data here*/},
 success: function(j)
 {
  //access your message like this
  alert(j.message)
 }
});

嵌套if语句也是不好的做法。你应该研究那个

希望这有帮助