
PHP form handling and validation in 2021
PHP form handling and validation
Form validation in PHP: Validation means to check the input data by the user is in the correct format or not, form validation is necessary to provide security of data. It is also important to protect your form data from hackers and spammers.
Validation rules for the field in PHP?
Name
1. It must contain only letters and white spaces.
2. It is required.
Contact
1. It must contain only letters.
2. It is required.
Email
1. It must be a valid email address with @ and dot(.).
2. It is required.
Website
1. It must be a valid URL.
2. It is required.
Validation on contact
<input type="number" name="txtcon"> |
Validation on email method 1
<input type="email" name="txtemail"> |
Validation on email method 2
filter_var() is a predefined function which is used to provide validation on email,url,IP address etc.
<input type="email" name="txtemail"> |
How to validation on email in PHP?
<?php $email = $_POST['txtemail']; if (filter_var($email, FILTER_VALIDATE_EMAIL) == true) { echo("$email is a valid "); } else { echo("$email is not a valid "); } ?> |
Validation on URL Method 1
filter_var() is a predefined function that is used to provide validation on email, URL, IP address, etc
How to validation URL in PHP?
<?php $url=$_POST['txturl']; if(filter_var($url,FILTER_VALIDATE_URL)==true) { echo "$url is valid"; } else { echo "URL is not valid"; } ?> |
Validation on URL Method 2
<?php $url =$_POST['txturl']; if (preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url)) { echo "URL is valid"; } else { echo "URL is not valid"; } ?> |
Form with Complete Validation
<!--PHP Code--> <?php if(isset($_POST['btn'])) { /*defining error variables and set them to empty */ $nameerr=$conerr=$emailerr=$weberr=$sererr="";
if(empty($_POST["txtn"])) $nameerr="* Name is
required";
if(empty($_POST["txtc"])) $conerr="* Contact
is required";
if(empty($_POST["txte"])) $emailerr="* Email
is required";
if(empty($_POST["txtweb"])) $weberr="* URL is
required";
if(empty($_POST["txts"])) $sererr="* Service
is required";
if(!empty($_POST["txtn"]) &&
!empty($_POST["txtc"]) && !empty($_POST["txte"])
&& !empty($_POST["txtweb"]) &&
!empty($_POST["txts"])) { /*passing form data into
php variables*/ $n=$_POST['txtn']; $c=$_POST['txtc']; $e=$_POST['txte']; $web=$_POST['txtweb']; $ser=$_POST['txts']; /*Validation on contact
number */ if(strlen($c)!=10) $conerr="Invalid
contact number"; /*Validation on email*/ else
if(!filter_var($e,FILTER_VALIDATE_EMAIL)==true) $emailerr="Invalid
email address"; /*Validation on Website
URL*/ else
if(!filter_var($web,FILTER_VALIDATE_URL)==true) $emailerr="Invalid
URL address"; else { /*Printing the result*/ echo "Traning
center Details<br>"; echo
"Name=".$n."<br>"; echo
"Contact=".$c."<br>"; echo
"Email=".$e."<br>"; echo "Website
URL=".$web."<br>"; echo
"Services=".$ser."<br>"; } } } ?> <!--HTML Form--> <html> <head> <title></title> </head> <body> <form action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>"
method="post" enctype="multipart/form-data"> <table border="0" width="40%"
style="border:1px solid gray" cellpadding="5"> <tr><td colspan="2" align="center" style="font-size:25px">Training center Registration Form</td></tr> <tr><td>Center Name</td><td><input
type="text" name="txtn" /> <label style="color:red"><?php echo $nameerr;
?></label> </td></tr> <tr><td>Contact</td><td><input
type="number" name="txtc" /> <label style="color:red"><?php echo $conerr;
?></label> </td></tr> <tr><td>Email</td><td><input
type="email" name="txte" /> <label style="color:red"><?php echo
$emailerr; ?></label> </td></tr> <tr> <td>WebsiteURL</td><td><input
type="text" name="txtweb" /> <label style="color:red"><?php echo $weberr;
?></label> </td></tr> <tr><td>Services</td><td> <textarea cols="40" rows="5"
name="txts"> </textarea> <label style="color:red"><?php echo $sererr;
?></label> </td></tr> <tr><td></td><td><input
type="submit" name="btn" value="Submit"
/></td></tr> </table> </form> </body> </html> *****OUTPUT*****
|
It is also important to protect your form data from hackers and spammers.
Required or Null Validation: Required is a property or attribute that specifies that an input field must be filled out before submitting the form.
It works with textbox, passwordbox, textrea, radiobutton etc.
<!--PHP Code--> <?php if(isset($_POST['btn'])) { /*Passing form data into php variable*/ $n=$_POST['txtn']; $c=$_POST['txtc']; $e=$_POST['txte']; /*Printing the result*/ echo "Name=".$n."<br>"; echo "Contact=".$c."<br>"; echo "Email=".$e."<br>"; } ?> <!--HTML Form--> <html> <head> <title>Form handling</title> </head> <body> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data"> <table border="0" width="30%" style="border:1px solid gray" cellpadding="5"> <tr><td>Name</td><td><input type="text" name="txtn" required/></td></tr> <tr><td>Contact</td><td><input type="text" name="txtc" required/></td></tr> <tr><td>Email</td><td><input type="text" name="txte" required/></td></tr> <tr><td></td><td><input type="submit" name="btn" value="Submit" /></td></tr> </table> </form> </body> </html> *****OUTPUT***** Name=icoderweb |
Validation rules for field
Name
1. It must contain only letters and white spaces.
2. It is required.
Contact
1. It must contain only letters.
2. It is required.
Email
1. It must be a valid email address with @ and dot(.).
2. It is required.
Website
1. It must be a valid URL.
2. It is required.
Display the error message through PHP code
<!--PHP Code--> <?php if(isset($_POST['btn'])) { /*defining error variables and set them to empty */ $nameerr=$conerr=$emailerr=$generr=$addrerr=""; if(empty($_POST["txtn"])) $nameerr="* Name is required"; if(empty($_POST["txtc"])) $conerr="* Contact is required"; if(empty($_POST["txte"])) $emailerr="* Email is required"; if(empty($_POST["gen"])) $generr="* Gender is required"; if(empty($_POST["txta"])) $addrerr="* Address is required"; if(!empty($_POST["txtn"]) && !empty($_POST["txtc"]) && !empty($_POST["txte"]) && !empty($_POST["gen"]) && !empty($_POST["txta"])) { /*passing form data into php variables*/ $n=$_POST['txtn']; $c=$_POST['txtc']; $e=$_POST['txte']; $gen=$_POST['gen']; $addr=$_POST['txta']; /*Printing the result*/ echo "Name=".$n."<br>"; echo "Contact=".$c."<br>"; echo "Email=".$e."<br>"; echo "Gender=".$gen."<br>"; echo "Address=".$addr."<br>"; } } ?> <!--HTML Form--> <html> <head> <title></title> </head> <body> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data"> <table border="0" width="40%" style="border:1px solid gray" cellpadding="5"> <tr><td>Name</td><td><input type="text" name="txtn" /> <label style="color:red"><?php echo $nameerr; ?></label> </td></tr> <tr><td>Contact</td><td><input type="number" name="txtc" /> <label style="color:red"><?php echo $conerr; ?></label> </td></tr> <tr><td>Email</td><td><input type="email" name="txte" /> <label style="color:red"><?php echo $emailerr; ?></label> </td></tr> <tr><td>Gender</td><td> <input type="radio" value="Male" name="gen"/>Male <input type="radio" value="Female" name="gen"/>Female <label style="color:red"><?php echo $generr; ?></label> </td></tr> <tr><td>Address</td><td> <textarea cols="40" rows="5" name="txta"> </textarea> <label style="color:red"><?php echo $addrerr; ?></label> </td></tr> <tr><td></td><td><input type="submit" name="btn" value="Submit" /></td></tr> </table> </form> </body> </html>
|
Form in PHP: A form is a very important element of PHP by using form and PHP code we can store data into a database in a very simple way.
Creating a Simple HTML Form
<html> <head> <title>Form handling</title> </head> <body> <form action="intro.php" method="post" enctype="multipart/form-data"> <table border="0" width="30%" style="border:1px solid gray" cellpadding="5"> <tr><td>Name</td><td><input type="text" name="txtn" /></td></tr> <tr><td>Contact</td><td><input type="text" name="txtc" /></td></tr> <tr><td>Email</td><td><input type="text" name="txte" /></td></tr> <tr><td></td><td><input type="submit" name="btn" value="Submit" /></td></tr> </table> </form> </body> </html> |
1. Here we are using the post method to send the data.
2. The variables name of the name, contact, and email textboxes are txtn,txtc and txte.
3. After filling the form when we will click submit button then form data will be sent to the intro.php file for processing.
Intro.php
<html> <head> <title>intro file</title> </head> <body> <?php /*Passing form data into php variable*/ $n=$_POST['txtn']; $c=$_POST['txtc']; $e=$_POST['txte']; /*Printing the result*/ echo "Name=".$n."<br>"; echo "Contact=".$c."<br>"; echo "Email=".$e."<br>"; ?> </body> </html> |
Intro.php
<?php if(isset($_POST['btn'])) { /*Passing form data into php variable*/ $n=$_POST['txtn']; $c=$_POST['txtc']; $e=$_POST['txte']; /*Printing the result*/ echo "Name=".$n."<br>"; echo "Contact=".$c."<br>"; echo "Email=".$e."<br>"; } ?> <!--HTML Form--> <html> <head> <title>Form handling</title> </head> <body> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data"> <table border="0" width="30%" style="border:1px solid gray" cellpadding="5"> <tr><td>Name</td><td><input type="text" name="txtn" /></td></tr> <tr><td>Contact</td><td><input type="text" name="txtc" /></td></tr> <tr><td>Email</td><td><input type="text" name="txte" /></td></tr> <tr><td></td><td><input type="submit" name="btn" value="Submit" /></td></tr> </table> </form> </body> </html> |
Complete form in PHP: Here I am going to make a complete form of Student Registration in which I am using required validation and filter validation for Validating Email. I am trying to keep all the important form elements in a single form.
How to fill PHP complete form?
<!--PHP Code--> <?PHP if(isset($_POST['btn'])) { /*passing form data into
php variables*/ $n=$_POST['txtn']; $c=$_POST['txtc']; $e=$_POST['txte']; $gen=$_POST['gen']; $dob=$_POST['dob']; $city=$_POST['ddlcity']; $addr=$_POST['txta']; /*getting education data
from checkbox*/ $edu="";
if(isset($_POST['chk10'])) $edu=$edu+"
10TH";
if(isset($_POST['chk12'])) $edu=$edu+"
12TH"; if(isset($_POST['BCA'])) $edu=$edu+"
BCA"; if(isset($_POST['MCA'])) $edu=$edu+"
MCA"; if(isset($_POST['B
Tech'])) $edu=$edu+" B
Tech"; if(isset($_POST['M
Tech'])) $edu=$edu+" M
Tech"; /*Validation on contact
number */ if(strlen($c)!=10) /*This message will
display in popup box Because we are using
alert function which is function of
javascript*/ echo
"<script>alert('Invalid contact number')</script>"; /*Validation on email*/ else
if(!filter_var($e,FILTER_VALIDATE_EMAIL)==true) echo
"<script>alert('Invalid email address')</script>"; else { /*Printing the result*/ echo "Student
Details<br>"; echo
"Name=".$n."<br>"; echo
"Contact=".$c."<br>"; echo
"Email=".$e."<br>"; echo
"Gender=".$gen."<br>"; echo
"DOB=".$dob."<br>"; echo
"City=".$city."<br>"; echo
"Address=".$addr."<br>"; echo
"Education=".$edu."<br>"; } } ?> <!--HTML Form--> <html> <head> <title></title> </head> <body> <form action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>"
method="post" enctype="multipart/form-data">
<table border="0" width="40%"
style="border:1px solid gray;font-weight:bold;font-family:arial;background-color:#CCCCCC"
cellpadding="5" > <tr><td colspan="2" align="center"
style="font-size:25px"> Student Registration Form</td></tr> <tr><td>Name</td><td><input
type="text" name="txtn" required/> </td></tr> <tr><td>Contact</td><td><input
type="number" name="txtc" required/> </td></tr> <tr><td>Email</td><td><input
type="email" name="txte" required/> </td></tr> <tr><td>Gender</td><td><input
type="radio" name="gen" value="Male"
/>Male<input type="radio" name="gen" value="Female"
/>Female </td></tr> <tr><td>DOB</td><td><input
type="date" name="dob" required/> </td></tr> </td></tr> <tr><td>City</td><td> <select name="ddlcity"> <option>Mumbai</option> <option>Delhi</option> <option>Pune</option> <option>Nagpur</option> <option>Patna</option> </select> </td></tr> <tr><td>Address</td><td> <textarea cols="40" rows="5"
name="txta" required> </textarea> </td></tr> <tr><td>Education</td><td> <input type="checkbox" name="chk10"
/>10th<br /> <input type="checkbox" name="chk12" />12th<br
/> <input type="checkbox" name="BCA"
/>BCA<br /> <input type="checkbox" name="MCA"
/>MCA<br /> <input type="checkbox" name="B Tech"
/>B Tech<br /> <input type="checkbox" name="M Tech"
/>M Tech<br /> </td></tr> <tr><td></td><td><input
type="submit" name="btn" value="Submit"
/></td></tr> </table> </form> </body> </html> *****OUTPUT***** |