每次都返回哎呀!必须输入所有输入。仔细检查表格。
它应该返回下一个错误,但我猜它没有识别我的 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">×</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>