所以我试图从我的服务器端php请求返回错误,但它返回整个页面的HTML内容。我有一个登录脚本,它做同样的事情并且工作,所以我开始变得非常沮丧。
我希望有人可以帮助我。
这是HTML:
<form id="register-form">
<h2>Register</h2>
<p><input type="text" class="form-control" id="username" name="username" placeholder="Username" autocomplete="off" /></p>
<p><input type="text" class="form-control" id="email" name="email" placeholder="Email" autocomplete="off" /></p>
<p><input type="password" class="form-control" id="password" name="password" placeholder="Password" autocomplete="off" /></p>
<p><input type="password" class="form-control" id="passwordConfirm" name="passwordConfirm" placeholder="Confirm Password" autocomplete="off" /></p>
<!-- DISPLAY ERRORS -->
<p id="error" class="text-danger"></p>
<input id="register-btn" type="submit" class="btn btn-lg btn-primary btn-block" name="registerBtn" value="Register Now" />
</form>
AJAX请求:
<script>
$('#register-btn').click(function(evt) {
evt.preventDefault();
var username = $('#username').val();
var email = $('#email').val();
var password = $('#password').val();
var passwordConfirm = $('#passwordConfirm').val();
$.post("register.php", { username: username, email: email, password: password, passwordConfirm: passwordConfirm }, function(data) {
alert(data);
//$('#error').html(data);
});
});
</script>
PHP:
if (isset($_POST['username']) && empty($_POST['email']) && empty($_POST['password']) && empty($_POST['passwordConfirm'])){
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = trim($_POST['password']);
$passwordConfirm = trim($_POST['passwordConfirm']);
if(empty($username) || empty($email) || empty($password) || empty($passwordConfirm)){
echo 'All fields are required.';
exit;
}else if ($member->member_exists($username) === true) {
echo 'That username is already taken.';
}
exit;
}