PHP/Ajax没有返回正确的错误并且没有识别POST变量


PHP/Ajax not returning proper error and not recognizing POST vars?

每次它都会返回哎呀必须输入所有输入。仔细检查表格。

它应该返回行中的下一个错误,但我猜它没有识别我的POST变量?为什么会这样?

以下代码:

PHP脚本:

if(isset($_POST['action'])) {
    switch($_POST['action']) {
        case 'register':
        $email_adress = @$_POST['register_email_address'];
        $password = hash('sha512', @$_POST['register_password']);
        $confirm_password = hash('sha512', @$_POST['register_confirm_password']);
        $safe_pin = @$_POST['register_safe_pin'];
            if(isset($email_address, $password, $confirm_password, $safe_pin)) {
                if(filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
                    if($password == $confirm_password) {
                        if(strlen($safe_pin) == 4 && is_numeric($safe_pin)) {
                            if(strlen($_POST['register_password']) >= 6 && strlen($_POST['register_confirm_password']) >= 6) {
                                $insert_user = $db->prepare("INSERT INTO `users`
                                (`email_address`, `password`, `safe_pin`, `time_registered`, `activated`, `balance`, `last_login, `ip_address`, `last_ip_address`)
                                VALUES (:email_address, :password, :safe_pin, :time_registered, :activated, :balance, :last_login, :ip_address, :last_ip_address)");
                                    $insert_user->execute(array(
                                        ':email_address'        => $email_address,
                                        ':password'             => $password,
                                        ':safe_pin'             => $safe_pin,
                                        ':time_registered'      => time(),
                                        ':activated'            => 0,
                                        ':balance'              => 0,
                                        ':last_login'           => 0,
                                        ':ip_address'           => $_SERVER['REMOTE_ADDR'],
                                        ':last_ip_address'      => $_SERVER['REMOTE_ADDR']
                                    ));
                            }
                            else { echo json_encode(array('result'  => '<strong>Oops!</strong> Your password must be a minimum of 6-characters.')); }
                        }
                            else { echo json_encode(array('result'  => '<strong>Oops!</strong> Your safe-pin must be a 4-character numeric code.')); }
                    }
                            else { echo json_encode(array('result'  => '<strong>Oops!</strong> Your confirmation password must match identically to your password.')); }
                }
                            else { echo json_encode(array('result'  => '<strong>Oops!</strong> Your e-mail address must be valid.  Make sure it''s typed properly. <br />')); }
            }
                            else { echo json_encode(array('result'  => '<strong>Oops!</strong> All inputs must be entered.  Double-check the form. <br />')); }
        break;
   }
}

jQuery/Javascript

<script type="text/javascript">
  $("#sign_up").on('click', function() {
    $.post('./includes/ajax.php', { action: 'register' } , function(result) {
      var result = JSON.parse(result);
        console.log(result.result);
        $("#register_result").append(result.result);
    });
  });
$("#register_form").submit(function() {
  return false;
});
$(".alert").hide();
$("#sign_up").on('click', function() { $(".alert").show(); });
</script>

HTML代码:

<div id="register_form">
              <form class="bs-example form-horizontal" id="register_form" method="POST" action="./includes/ajax.php">
                <fieldset>
                  <legend>Create a Wallet</legend>
                      <div class="alert alert-dismissable alert-danger">
              <button type="button" class="close" data-dismiss="alert">&times;</button>
             <span id="register_result"></span>
            </div>
                  <div class="form-group">
                    <label for="inputEmail" class="col-lg-2 control-label">Email</label>
                    <div class="col-lg-10">
                      <input type="text" class="form-control" id="inputEmail" placeholder="Your E-mail Address." name="register_email_address">
                    </div>
                  </div>
                  <div class="form-group">
                    <label for="inputPassword" class="col-lg-2 control-label">Password</label>
                    <div class="col-lg-10">
                      <input type="password" class="form-control" id="inputPassword" placeholder="Your 6-character password." name="register_password">
                    </div>
                  </div>
                      <div class="form-group">
                    <label for="inputPassword" class="col-lg-2 control-label">Confirm</label>
                    <div class="col-lg-10">
                      <input type="password" class="form-control" id="inputPassword" placeholder="Your 6-character password." name="register_confirm_password">
                    </div>
                  </div>
                        <div class="form-group">
                    <label for="inputPassword" class="col-lg-2 control-label">Safe-Pin</label>
                    <div class="col-lg-10">
                      <input type="password" class="form-control" id="inputPassword" placeholder="Your 4-digit safe-pin." name="register_safe_pin">
                    </div>
                  </div>
                  <div class="form-group">
                    <div class="col-lg-10 col-lg-offset-2">
                      <button type="submit" class="btn btn-primary" id="sign_up">Sign Up</button> 
                    </div>
                  </div>
                </fieldset>
              </form>
            </div>

您没有将表单传递给注册脚本。您必须序列化表单的内容并发送请求后的

$("#sign_up").on('click', function() {
    $.post('./includes/ajax.php', $('#register_form').serialize() + '&action=register' , function(result) {
        var result = JSON.parse(result);
        console.log(result.result);
        $("#register_result").append(result.result);
    });
});

我现在更新了我的全部答案。。。因为您的代码中有超过1个错误。

首先要考虑的是,购买一个用于突出显示错误的IDE(软件)。这可以让你的生活变得如此轻松。

尽可能经常地显示你的代码,上下反复阅读,注释出一些代码行来测试哪里出了问题。。。等等

我认为这个代码现在是绝对没有错误和清晰的。

我改变了什么:

  1. $email_adress$email_address

  2. if (strlen($_POST['register_password']) >= 6 && strlen($_POST['register_confirm_password']) >= 6) {...

    if (strlen($_POST['register_password']) > 5 && strlen($_POST['register_confirm_password']) > 5) {...

  3. :password:pwd我更改了,因为这可能是一个保留字,而且不太明显(可以随意更改)

...`last_login, `ip_address`,...

到(忘记倒勾)

...`last_login`, `ip_address`,...

以及整个代码:

if (isset($_POST['action'])) {
    switch($_POST['action']) {
    case 'register':
        $email_address      = @$_POST['register_email_address'];
        $password           = hash('sha512', @$_POST['register_password']);
        $confirm_password   = hash('sha512', @$_POST['register_confirm_password']);
        $safe_pin           = @$_POST['register_safe_pin'];
        if (isset($email_address, $password, $confirm_password, $safe_pin)) {
            if (filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
                if ($password == $confirm_password) {
                    if (strlen($safe_pin) == 4 && is_numeric($safe_pin)) {
                        if (strlen($_POST['register_password']) > 5 && strlen($_POST['register_confirm_password']) > 5) {
                            $insert_user = $db->prepare("INSERT INTO `users` (`email_address`, `pwd`, `safe_pin`, `time_registered`, `activated`, `balance`, `last_login`, `ip_address`, `last_ip_address`)
                                                         VALUES (:email_address, :pwd, :safe_pin, :time_registered, :activated, :balance, :last_login, :ip_address, :last_ip_address)");
                            $insert_user->execute(array(
                                ':email_address'        => $email_address,
                                ':pwd'                  => $password,
                                ':safe_pin'             => $safe_pin,
                                ':time_registered'      => time(),
                                ':activated'            => 0,
                                ':balance'              => 0,
                                ':last_login'           => 0,
                                ':ip_address'           => $_SERVER['REMOTE_ADDR'],
                                ':last_ip_address'      => $_SERVER['REMOTE_ADDR']
                            ));
                        }
                        else { echo json_encode(array('result'  => '<strong>Oops!</strong> Your password must be a minimum of 6-characters.')); }
                    }
                    else { echo json_encode(array('result'  => '<strong>Oops!</strong> Your safe-pin must be a 4-character numeric code.')); }
                }
                else { echo json_encode(array('result'  => '<strong>Oops!</strong> Your confirmation password must match identically to your password.')); }
            }
            else { echo json_encode(array('result'  => '<strong>Oops!</strong> Your e-mail address must be valid.  Make sure it''s typed properly. <br />')); }
        }
        else { echo json_encode(array('result'  => '<strong>Oops!</strong> All inputs must be entered.  Double-check the form. <br />')); }
        break;
    }
}

祝你好运!