我试图让我的表单在输入字段上执行验证并显示有错误的位置。相反,页面刷新为空白页面。任何帮助将不胜感激。
注意:这些文件位于同一个文件(registration.php)
中这是我的代码(PHP):
//DB information
require("config.php");
//define error variables
$usernameValidation = $passwordValidation = $emailValidation = $fNameValidation = $lNameValidation = $usernameTaken = "";
if(isset($_POST['submitSignUp']))
{
function validateSignUp()
{
$username = $_POST['userSignUp'];
$password = $_POST['passSignUp'];
$confPassword = $_POST['confPassSignUp'];
$email = $_POST['emailSignUp'];
$fName = $_POST['fName'];
$lName = $_POST['lName'];
$username = mysqli_real_escape_string($con, $_POST['userSignUp']);
$password = mysqli_real_escape_string($con, $_POST['passSignUp']);
$email = mysqli_real_escape_string($con, $_POST['emailSignUp']);
$fName = mysqli_real_escape_string($con, $_POST['fName']);
$lName = mysqli_real_escape_string($con, $_POST['lName']);
$errors = array();
//Validation on form
if(empty($username) || strlen($username) > 20){
$usernameValidation = "errorInput";
array_push($errors, "errorUsername");
}
if(empty($fName) || strlen($fName) > 20 || !preg_match("/^[a-zA-Z ]*$/",$fName)){
$fNameValidation = "errorInput";
array_push($errors, "errorFName");
}
if(empty($lName) || strlen($lName) > 30 || !preg_match("/^[a-zA-Z ]*$/",$lName)){
$lNameValidation = "errorInput";
array_push($errors, "errorLName");
}
if(empty($password) || empty($confPassword) || strlen($password) > 20 || strlen($confPassword) > 20 || $password != $confPassword){
$passwordValidation = "errorInput";
array_push($errors, "errorPassword");
}
if(empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)){
$emailValidation = "errorInput";
array_push($errors, "errorEmail");
}
//check if username is already taken
$sql = mysqli_query($con, "SELECT * FROM users WHERE username = '$username'");
$numRows = mysqli_num_rows($sql);
if($numRows > 0)
{
//username already exists
$usernameTaken = "That username is already taken";
array_push($errors, "errorUsernameTaken");
}
if(count($errors) > 0){
return false;
}
else{
//encrypt password
$password = md5($password);
//store info in DB
$result = mysqli_query($con, "INSERT INTO users (username, password, email, firstName, lastName) VALUES ('$username', '$password', '$email', '$fName', '$lName')");
//redirect to main page
header('Location: index.php');
}
}
}
else if(isset($_POST['submitSignIn']))
{
function validateSignIn()
{
$username = $_POST['userSignIn'];
$password = $_POST['passSignIn'];
$errors = array();
if(empty($username)){
$usernameValidation = "errorInput";
array_push($errors, "error");
}
if(empty($password)){
$passwordValidation = "errorInput";
array_push($errors, "error");
}
if(count($errors) > 0){
return false;
}
else{
$username = mysqli_real_escape_string($con, $_POST['userSignIn']);
$password = mysqli_real_escape_string($con, $_POST['passSignIn']);
$password = md5($password);
$sql = mysqli_query($con, "SELECT * FROM users WHERE username = '$username' AND 'password' = '$password'");
$result = mysqli_num_rows($sql);
if($result > 0)
{
//correct information, log them in
header('Location: index.php');
}
else
{
//incorrect info
return false;
}
}
}
}
这是我的代码(HTML):
<body>
<div class="login-wrap">
<div class="login-html">
<input id="tab-1" type="radio" name="tab" class="sign-in" checked><label for="tab-1" class="tab">Sign In</label>
<input id="tab-2" type="radio" name="tab" class="sign-up"><label for="tab-2" class="tab">Sign Up</label>
<div class="login-form">
<div class="sign-in-htm">
<form action="" method="POST">
<div class="group">
<label for="user" class="label">Username</label>
<input id="userSignIn" name="userSignIn" type="text" class="input <?php if(isset($_POST['userSignIn'])){ echo ($usernameValidation);}?>" value="<?php if(isset($_POST['userSignIn'])){ echo htmlentities($username);}?>">
</div>
<div class="group">
<label for="pass" class="label">Password</label>
<input id="passSignIn" name="passSignIn" type="password" class="input <?php if(isset($_POST['passSignIn'])){ echo ($passwordValidation);}?>" data-type="password">
</div>
<div class="group">
<input id="check" type="checkbox" class="check" checked>
<label for="check"><span class="icon"></span> Keep me Signed in</label>
</div>
<div class="group">
<input type="submit" name="submitSignIn" class="button" value="Sign In" onsubmit="return validateSignIn()">
</div>
<div class="hr"></div>
<div class="foot-lnk">
<a href="#forgot">Forgot Password?</a>
</div>
</form>
</div>
<div class="sign-up-htm">
<form action="" method="POST">
<div class="group">
<label for="fName" class="label">First Name</label>
<input id="fName" type="text" name ="fName" class="input <?php if(isset($_POST['fName'])){ echo ($fNameValidation);}?>" value="<?php if(isset($_POST['fName'])){ echo htmlentities($fName);}?>">
</div>
<div class="group">
<label for="lName" class="label">Last Name</label>
<input id="lName" type="text" name ="lName" class="input <?php if(isset($_POST['lName'])){ echo ($lNameValidation);}?>" value="<?php if(isset($_POST['lName'])){ echo htmlentities($lName);}?>">
</div>
<div class="group">
<label for="user" class="label">Username</label>
<input id="userSignUp" type="text" name ="userSignUp" class="input <?php if(isset($_POST['userSignUp'])){ echo ($usernameValidation);}?>" value="<?php if(isset($_POST['userSignUp'])){ echo htmlentities($username);}?>">
<span class="error"><?php echo $usernameTaken ?></span>
</div>
<div class="group">
<label for="pass" class="label">Password</label>
<input id="passSignUp" name="passSignUp" type="password" class="input <?php if(isset($_POST['passSignUp'])){ echo ($passwordValidation);}?>" data-type="password">
</div>
<div class="group">
<label for="pass" class="label">Confirm Password</label>
<input id="confPassSignUp" name="confPassSignUp" type="password" class="input <?php if(isset($_POST['passSignUp'])){ echo ($passwordValidation);}?>" data-type="password">
</div>
<div class="group">
<label for="pass" class="label">Email Address</label>
<input id="emailSignUp" name="emailSignUp" type="text" class="input <?php if(isset($_POST['emailSignUp'])){ echo ($emailValidation);}?>" value="<?php if(isset($_POST['emailSignUp'])){ echo htmlentities($email);}?>">
</div>
<div class="group">
<input type="submit" name="submitSignUp" class="button" value="Sign Up" onsubmit="return validateSignUp()">
</div>
<div class="hr"></div>
<div class="foot-lnk">
<label for="tab-1">Already Member?</a>
</div>
</form>
</div>
</div>
</div>
提前谢谢!!
答案 0 :(得分:0)
问题是,你没有调用任何一个函数
validateSignUp()
或validateSignUp()
您的代码就像......
if(isset($_POST['submitSignUp']))
{
function validateSignUp()
{
// ....
}
} else if(isset($_POST['submitSignIn'])) {
function validateSignIn()
{
// ....
}
}
相反......它应该像......
if(isset($_POST['submitSignUp']))
{
function validateSignUp()
{
// ....
}
validateSignUp(); // <--- Notice
} else if(isset($_POST['submitSignIn'])) {
function validateSignIn()
{
// ....
}
validateSignIn(); // <--- Notice
}
坦率地说,如果你要在整个代码中只使用它们一次,我会要求你从那里删除这两个函数......函数是针对多个用例和login /注册只是单一案例......