也许我完全错了,但我的目标是使用jquery、ajax和php来验证我的注册表。我目前已将其设置为:
用户在表单中输入数据
如果任何数据不正确,则使用ajax在表单下方显示一条错误消息
如果表单正确提交,数据将发送到数据库
几乎所有的事情看起来都很正常,但问题是如果表格填写正确,我会打电话给你
header("Location: ../index.php”);
然后它将我的索引页加载到register页的顶部,因为默认情况下不允许submit按钮将用户带到新页。就像我有两个网页在一个浏览器窗口上的一个。如果表格填写正确,我有没有办法“重新允许”提交按钮正常工作?
html格式:
<?php
include "shared/_header.php";
include "shared/_navBar.php";
?>
<div class="wrapper-register">
<div id="register-account">
<div class="register-title">
<h2>REGISTER ACCOUNT</h2>
</div>
<form action="shared/_registerAccount.php" method="post">
<div class="register-input">
<input id="register-username" type="text" name="username" maxlength="16" placeholder="Username" />
</div>
<div class="register-input">
<input id="register-email" type="text" name="email" maxlength="128" placeholder="Email" />
</div>
<div class="register-input">
<input id="register-password" type="password" name="password" maxlength="128" placeholder="Password" />
</div>
<div class="register-input">
<input id="register-confirm-password" type="password" name="confirmPassword" maxlength="128" placeholder="Confirm Password" />
</div>
<input id="register-submit" type="submit" name="submit" value="SIGN UP" class="register-button" />
</form>
<p class="form-message"></p>
</div>
</div>
<?php include "shared/_footer.php" ?>
<script src="javascript/register.js"></script>
</body>
</html>
jquery查询:
/// <reference path="jquery-3.3.1.min.js" />
$(document).ready(function() {
$("form").submit(function(event) {
event.preventDefault();
var username = $("#register-username").val();
var email = $("#register-email").val();
var password = $("#register-password").val();
var confirmPassword = $("#register-confirm-password").val();
var submit = $("#register-submit").val();
$(".form-message").load("../shared/_registerAccount.php", {
username: username,
email: email,
password: password,
confirmPassword: confirmPassword,
submit: submit
});
});
});
PHP:
<?php
if (isset($_POST['submit']))
{
include_once "_databaseHandler.php";
$username = mysqli_real_escape_string($connection, $_POST['username']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$password = mysqli_real_escape_string($connection, $_POST['password']);
$confirmPassword = mysqli_real_escape_string($connection,
$_POST['confirmPassword']);
$errorEmpty = false;
//Check for empty fields
if (empty($username) || empty($email) || empty($password) ||
empty($confirmPassword))
{
echo "<span class='form-error'>Please fill in all fields</span>";
$errorEmpty = true;
}
else
{
$sqlUsername = "SELECT * FROM userinfo WHERE userName = '$username'";
$resultUsername = mysqli_query($connection, $sqlUsername);
$resultCheckUsername = mysqli_num_rows($resultUsername);
$sqlEmail = "SELECT * FROM userinfo WHERE userEmail = '$email'";
$resultEmail = mysqli_query($connection, $sqlEmail);
$resultCheckEmail = mysqli_num_rows($resultEmail);
//Check if username is taken
if ($resultCheckUsername > 0)
{
echo "<span class='form-error'>This username is already
taken</span>";
$errorEmpty = true;
}
//Check if username has invalid characters
else if (preg_match("/^[0-9a-zA-Z]$/", $username))
{
echo "<span class='form-error'>This username contains invalid
characters</span>";
$errorEmpty = true;
}
//Check if email is valid
else if ($resultCheckEmail > 0)
{
echo "<span class='form-error'>This email is already in
use</span>";
$errorEmpty = true;
}
//Check if email is already in use
else if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo "<span class='form-error'>Please enter a valid email
address</span>";
$errorEmpty = true;
}
//Check if passwords match
else if ($_POST['password'] != $_POST['confirmPassword'])
{
echo "<span class='form-error'>Passwords do not match</span>";
$errorEmpty = true;
}
//Insert the user into the database
else
{
$sql = "INSERT INTO userinfo (userName, userEmail, userPassword)
VALUES (?, ?, ?);";
$statement = mysqli_stmt_init($connection);
if (!mysqli_stmt_prepare($statement, $sql))
{
echo "<span>Database error</span>";
header("Location: ../index.php");
exit();
}
else
{
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($statement, "sss", $username, $email,
$hashedPassword);
mysqli_stmt_execute($statement);
header("Location: ../index.php?signup=success");
exit();
}
}
}
}
else
{
header('Location: ../index.php');
exit();
}
?>
<script>
$("#username, #email, #password, #confirmPassword").removeClass(".input-
error").removeClass(".input-error");
var errorEmpty = "<?php echo $errorEmpty; ?>";
if (errorEmpty)
{
$("#username, #email, #password, #confirmPassword").addClass(".input-
error");
}
if (!errorEmpty)
{
$("#username, #email, #password, #confirmPassword").val("");
}
</script>
暂无答案!
目前还没有任何答案,快来回答吧!