我正在尝试使用ajax重定向页面以验证我的表单。问题是,当我点击注册按钮时,我得到一个包含文字的页面,而不是重定向或验证:
{"status":true,"message":"error","redirect":"..\/login-error-page.php"}
在我的错误日志中:
[07-Jun-2017 01:04:24 UTC] PHP Notice: Undefined variable: errorEmpty in include/signup.inc.php
[07-Jun-2017 01:04:24 UTC] PHP Notice: Undefined variable: errorEmail in include/signup.inc.php
谁能告诉我什么是错的?我是ajax的新手。提前谢谢。
的index.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Yahbang</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
$(document).on('submit', '.signup-message', function () {
$("#signup-form").submit(function (event) {
event.preventDefault();
var first = $("#signup-first").val();
var last = $("#signup-last").val();
var email = $("#signup-email").val();
var pwd = $("#signup-pwd").val();
var submit = $("#signup-submit").val();
$.ajax({
url: 'include/signup.inc.php',
type: 'POST',
data: {first: first, last: last, email: email, pwd: pwd, submit: submit},
datatype: 'json',
success: function (respond) {
if (!respond.status) {
alert(respond.message);
}
else
Window.Location.href = respond.redirect;
}
});
});
});
</script>
</head>
<body>
<header>
<div class="header_index">
<div class="headerlogo">
<nav>
<ul>
<li><a href="index.php">Yahbang</a></li>
</ul>
</nav>
</div>
<form id="login-form" class="loginform" action='include/login.inc.php' method='POST'>
<input id="login-email" type='text' name='email' placeholder='Email'>
<input id="login-pwd" type='password' name='pwd' placeholder='Password'>
<p><a href="forgotpassword.php">Forgot Password</a></p>
<button id="login-submit" type='submit'>Login</button>
<p class="login-message"></p>
</form>
</div>
</header>
<form id="signup-form" class="signup" action='include/signup.inc.php' method='POST'>
<input id="signup-first" type='text' name='first' placeholder='First Name'><br>
<input id="signup-last" type='text' name='last' placeholder='Last Name'><br>
<input id="signup-email" type='text' name='email' placeholder='Email'><br>
<input id="signup-pwd" type='password' name='pwd' placeholder='Password'><br>
<button id="signup-submit" type='submit'>Sign Up</button>
<p class="signup-message"></p>
</form>
<footer>
<div class="footer_index">
<nav>
<ul>
<li><a href="contact.php">Contact Us</a></li>
<li><a href="TermsofUse.php">Terms of Use</a></li>
</ul>
</nav>
</div>
</footer>
</body>
</html>
signup.inc.php:
<?php
session_start();
include '../dbh.php';
$respond = [
'status' => true,
'message' => 'error',
'redirect' => '../profile.php',
];
echo json_encode($respond);
if (isset($_POST['submit'])) {
$first = mysqli_real_escape_string($conn, $_POST['first']);
$last = mysqli_real_escape_string($conn, $_POST['last']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);
$errorEmpty = false;
$errorEmail = false;
if (empty($first) || empty($last) || empty($email) || empty($pwd)) {
echo "<span class='signup-error'>Please fill out all fields!</span>";
$errorEmpty = true;
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<span class='signup-error'>Please enter a valid email address!</span>";
$errorEmail = true;
} else {
$sql = "SELECT email FROM user WHERE email='$email'";
$result = mysqli_query($conn, $sql);
$emailcheck = mysqli_num_rows($result);
if ($emailcheck > 0) {
echo "<span class='signup-error'>That email address already exists!</span>";
$errorEmail = true;
} else {
$encryptpwd = password_hash($pwd, PASSWORD_DEFAULT);
$sql = "INSERT INTO user (first, last, email, pwd)
VALUES ('$first', '$last', '$email', '$encryptpwd')";
$result = mysqli_query($conn, $sql);
}
}
}
?>
<script>
$("#signup-first, #signup-last, #signup-email, #signup-pwd").removeClass("input-error");
var errorEmpty = "<?php echo $errorEmpty; ?>";
var errorEmail = "<?php echo $errorEmail; ?>";
if (errorEmpty == true) {
$("#signup-first, #signup-last, #signup-email, #signup-pwd").addClass("input-error");
}
if (errorEmail == true) {
$("#signup-email").addClass("input-error");
}
if (errorEmpty == false && errorEmail == false) {
$("#signup-first, #signup-last, #signup-email, #signup-pwd").val("");
}
</script>
答案 0 :(得分:0)
您尝试在ajax中添加error
以模拟来自php的错误响应
$.ajax({
url: 'include/signup.inc.php',
type: 'POST',
data: {first: first, last: last, email: email, pwd: pwd, submit: submit},
datatype: 'json',
success: function(respond){
if (!respond.status)
{
alert(respond.message);
}
else
Window.Location.href = respond.redirect;
},
error: function(error){
//methods here
}
});