PHP Code part in Ajax form validation

In the first part of form validation we have seen how the Ajax form and the functions we have used to collect data from forms and then process it to handover to PHP file by using POST method. After getting the data we will write PHP code to validate the data and generate the message.

We have started with collecting the data, as we have used POST method of form submission so we used same technique to collect all four variables used in the form.

@$fname=$_POST['fname'];
@$sex=$_POST['sex'];
@$month=$_POST['month'];
@$agree=$_POST['agree'];

We set a flag to status “OK” and at the end we will see if the $status_form is changed or not, if any of the validation fails then this status will change to NOTOK. We will also crate a variable to store the message and at the beginning it is kept blank.

$status_form = "OK";// Set a flag to check
$msg=""; // Message variable

$row=array("t1"=>"T","r1"=>"T","r2"=>"T","s1"=>"T","c1"=>"T","msg"=>"","status_form"=>"OK");

Related Tutorial
Ajax Validation onBlur
We have created one array with all the element status setting to T or validation is passed. As we check one by one element , we will change the status to false and add failure message to it.
Now we will start with checking the data which as come to us as fname for First name. We have set a validation check as it must me minimum 3 char in length. You can change your validation checking as per your requirement. It can be alphanumeric or it can be only alphabets or you can add maximum length to this. We have used strlen function to check the length of the entered data.

if(strlen($fname) < 3){
$row["status_form"]="NOTOK";
$row["t1"]="F";
$msg .= "Your Name must be more than 3 char length<br>";
}

See that if the valdation is not OK then we are changing the $status_form and the message we are adding the respective elements of the array Similar way we have checked that the user has selected one of the two radio button by checking the variable sex. User must select one of the month so we have checked the variable $month. Finally we can check the status of the checkbox by checking its value is set to yes or not. If all the validations passed then the status of variable $status is checked ( OK or NOTOK) and output is given in green color font or in red color font.

Here is the demo of PHP Ajax form validation

This is a simple example on how to validate the form data using Ajax and PHP. But this is not complete code. We know after validation if the data is OK then we need to send the status message back to form. Now we can reset the form or we can redirect the page to another page with thank you message. How to do that ? Here so far we have seen how to send back a single line of message to main page , now we need to send another data saying the status of the validation. This we will learn in next part.

Download the code of this demo


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    Post your comments , suggestion , error , requirements etc here .




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer