注册结果不断返回false

时间:2014-08-24 06:05:55

标签: php jquery ajax

我已经创建了一个AJAX寄存器,每次按下注册按钮都会返回false。

这是我正在使用的PHP代码:

<?php
try {
    require('db.php');
    require_once('recaptchalib.php');
    $privatekey = "HIDDEN!";

    $resp = recaptcha_check_answer ($privatekey,
        $_SERVER["REMOTE_ADDR"],
        $_POST["recaptcha_challenge_field"],
        $_POST["recaptcha_response_field"]);

    if(!$resp->is_valid) {
        echo 'wrong_captcha';
    } else {
        $email = $_POST['email'];
        $password = $_POST['password'];
        $password_confirm = $_POST['password_confirm'];
        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];

        if($password != $password_confirm) {
            echo 'no_match';
        } else {
            $pass = password_hash($password, PASSWORD_BCRYPT);

            $SQL = $dbh->prepare('INSERT INTO users (email, password, first_name, last_name) VALUES (:email, :password, :first_name, :last_name)');
            $SQL->execute(array(
                ':email'=>$email,
                ':password'=>$pass,
                ':first_name'=>$first_name,
                ':last_name'=>$last_name
            ));
            $result = $SQL->execute();

            if($result) {
                echo 'true';
            } else {
                echo 'false';
            }
        }
    }
} catch (PDOException $e) {
    echo $e->getMessage();
}
?>

然而,当我发现错误时,AJAX返回false,我不是100%肯定,但为了以防万一,这里是AJAX代码:

$(document).ready(function() {
$('#register_button').click(function() {
    email = $('#email').val();
    password = $('#password').val();
    password_confirm = $('#password_confirm').val();
    first_name = $('#first_name').val();
    last_name = $('#last_name').val();
    $.ajax({
        type: 'POST',
        url: 'inc/register.php',
        data: 'email='+email+'&password='+password+'&password_confirm='+password_confirm+'&first_name='+first_name+'&last_name='+last_name,
        success: function(html) {
            if(html == 'true') {
                $('#error').fadeOut();
                $('#wrong_captcha').fadeOut();
                $('#success').fadeIn();
            } else if(html == 'wrong_captcha') {
                $('#wrong_captcha').fadeIn();
            } else if(html == 'no_match') {
                $('#no_match').fadeIn();
            } else {
                $('#error').fadeIn();
            }
        },
        beforeSend: function() {
            //loading
        }
    });
    return false;
});
});

2 个答案:

答案 0 :(得分:1)

代码应

try{
    $result = $SQL->execute(array(
                    ':email'=>$email,
                    ':password'=>$pass,
                    ':first_name'=>$first_name,
                    ':last_name'=>$last_name
                ));
}catch(PDOException $e){
echo $e->getMessage();
}

而不是

    $SQL->execute(array(
        ':email'=>$email,
        ':password'=>$pass,
        ':first_name'=>$first_name,
        ':last_name'=>$last_name
    ));
    $result = $SQL->execute();

你的AJAX代码总是返回false。试试这个

$.ajax({
        type: 'POST',
        url: 'inc/register.php',
        data: 'email='+email+'&password='+password+'&password_confirm='+password_confirm+'&first_name='+first_name+'&last_name='+last_name,
        success: function(html) {
      if(html == 'true') {
                    $('#error').fadeOut();
                    $('#wrong_captcha').fadeOut();
                    $('#success').fadeIn();
                    return true;
                } else if(html == 'wrong_captcha') {
                    $('#wrong_captcha').fadeIn();
                } else if(html == 'no_match') {
                    $('#no_match').fadeIn();
                } else {
                    $('#error').fadeIn();
                }
            },
            beforeSend: function() {
                //loading
            }
            error:function(){
             return false;   
        }
        });

答案 1 :(得分:1)

也许你应该重新检查你的Captcha。尝试从谷歌重拍它。 另外,请尝试检查您的数据库信息。 最后一个,如果您想创建一个AJAX表单,请转到此网站~~&gt; http://tutorialzine.com/2009/10/cool-login-system-php-jquery/