Ashish Mehra Ashish Mehra - 5 months ago 13
Ajax Question

Empty feild show as error every time I submit form using AJAX

Hey all i am new in programming field especially in php and I try to make a code for form validation and submition using ajax but when i submit the form it always show me error as empty field.enter image description here

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 ='&#xea0f;'>".$error."</span>";
}

Answer

in your ajax call you not pass email id

 data : 'firstName='+firstName + '&lastName='+lastName + '&userName='+userName + '&password='+password + '&con_password='+con_password,

so pass email id in ajax call use this

data : 'firstName='+firstName + '&lastName='+lastName + '&userName='+userName + '&password='+password + '&con_password='+con_password+'&email='+email,

i hope it will be helpful