-1

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

它应该返回下一个错误,但我猜它没有识别我的 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>
4

2 回答 2

1

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

$("#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);
    });
});
于 2013-09-02T12:19:11.630 回答
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. :passwordto :pwdI changed 因为它可以是保留字,而且不是很明显(随意更改)

...`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;
    }
}

祝你好运!

于 2013-09-02T00:35:10.963 回答