我尝试使用ajax为表单验证和提交创建代码,但是当我提交表单时,它总是将错误显示为空字段。
的index.php
<form action="" method="post" id="#user_reg">
<!--printing error or success message -->
<li>
<input type="text" name="firstName" id="firstName" placeholder="First Name"
<?php
//restoring values
if(!empty($error)){
echo "value = ".$firstName;
}
?>
>
</li>
<li>
<input type="text" name="lastName" id="lastName" placeholder="Last Name"
<?php
//restoring values
if(!empty($error)){
echo "value = ".$lastName;
}
?>
>
</li>
<li>
<input type="text" name="userName" id="userName" placeholder="Username (must be greater than 5 characters)"
<?php
//restoring values
if(!empty($error)){
echo "value = ".$userName;
}
?>
>
</li>
<li>
<input type="email" name="email" id="email" placeholder="Email"
<?php
//restoring values
if(!empty($error)){
echo "value = ".$email;
}
?>
>
</li>
<li>
<input type="password" name="password" id="password" placeholder="Password (must be greater than 8 characters)">
</li>
<li>
<input type="password" name="con_password" id="con_password" placeholder=" Confirm Password">
</li>
<li>
<input type="button" name="submit" id="reg" value="Register">
</li>
</form>
AJAX
$('#reg').click(function(){
//variable for storing post data
var firstName = $('#firstName').val();
var lastName = $('#lastName').val();
var userName = $('#userName').val();
var email = $('#email').val();
var password = $('#password').val();
var con_password = $('#con_password').val();
//making an ajax request
$.ajax({
url : "request/register.php",
type : "POST",
data : 'firstName='+firstName + '&lastName='+lastName + '&userName='+userName + '&password='+password + '&con_password='+con_password,
dataType : "text",
success : function(response,status,http){
$('#show').html(response);
},
error : function(http,status,error){
alert('server error');
}
}) })
register.php
$success = "Submitted";
if($_SERVER['REQUEST_METHOD'] == "POST"){
$firstName = trim(filter_input(INPUT_POST, 'firstName' ,FILTER_SANITIZE_STRING));
$lastName = trim(filter_input(INPUT_POST, 'lastName' ,FILTER_SANITIZE_STRING));
$userName = trim(filter_input(INPUT_POST, 'userName' ,FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST, 'email' ,FILTER_SANITIZE_EMAIL));
$password = trim(filter_input(INPUT_POST, 'password' ,FILTER_SANITIZE_STRING));
$confirm_pass = trim(filter_input(INPUT_POST, 'con_password' ,FILTER_SANITIZE_STRING));
//checking for empty feilds
if($firstName == "" || $lastName == "" || $userName == "" || $email == "" || $password == "" || $confirm_pass == ""){
$error = "Empty Fields";
}
//checking username length
if(empty($error) && strlen($userName)<=5){
$error = "Username must be greater than 5 characters";
}
//checking for username existence
if(empty($error) && user_exist($userName)){
$error = "Username already exist";
}
//email validation
if(empty($error) && !filter_var($email,FILTER_VALIDATE_EMAIL)){
print_r($_POST);
$error = "Invalid Email address";
}
//checking for email existence
if(empty($error) && email_exist($email)){
$error = "Email already exist";
}
//checking password length
if(empty($error) && strlen($password)<=8){
$error = "Password must be greater than 8 characters";
}
//matching confirm password
if(empty($error) && $password !== $confirm_pass){
$error = "Password not match";
}
if(empty($error)){
if(user_registration($firstName,$lastName,$userName,$email,md5($password))){
header("location:index.php?registered");
exit();
}else{
$error = "Something went wrong";
}
} }
if(!empty($error)){
echo "<span class=\"error\" data-icon =''>".$error."</span>";
}
答案 0 :(得分:0)
在你的ajax电话中你没有传递电子邮件ID
data : 'firstName='+firstName + '&lastName='+lastName + '&userName='+userName + '&password='+password + '&con_password='+con_password,
所以在ajax调用中传递电子邮件ID 使用这个
data : 'firstName='+firstName + '&lastName='+lastName + '&userName='+userName + '&password='+password + '&con_password='+con_password+'&email='+email,
我希望它会有所帮助