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

时间:2013-09-02 00:20:44

标签: php javascript jquery html ajax

每次返回糟糕!必须输入所有输入。仔细检查表格。

它应该返回下一个错误,但我想它不能识别我的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>

2 个答案:

答案 0 :(得分:1)

您没有将表单传递给您的注册脚本。您必须serialize表单内容并发送帖子请求

$("#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 :(得分:0)

我现在更新了我的答案...因为你的代码中有超过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我改变了,因为这可能是一个保留字而且不是那么明显(根据需要改变它)

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

    to(忘记反击)

    ...`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;
        }
    }
    
    祝你好运!