Form Validation in PHP
Today we will make an application that will check your form empty values.
For this purpose we will design an email form that contains following fields
- Your Name
- Your Email
- Sender Email
- Comments :
- Submit Button:
Once we click on submit button, it will check your fields, if all fields are correct, the form will be submitted correctly, otherwise form shows errors like field is empty, email is not correct etc.
We will use if else condition like:
if (condition)
{do something}
else
{ do this}
{do something}
else
{ do this}
Now we will make a email, let's write code.
<h1>Email Form</h1>
<form name="form1" method="post" action="">
<p>You Name:
<input name="name" type="text" id="name">
<br>
You Email:
<input name="email" type="text" id="email">
</p>
<p>Sender Email:
<input name="sender_email" type="text" id="sender_email">
<br>
Comments<br>
<textarea name="comments" cols="50" rows="10" id="comments"></textarea>
</p>
<p>
<input name="submit" type="submit" id="submit" value="Submit">
</p>
</form>
<form name="form1" method="post" action="">
<p>You Name:
<input name="name" type="text" id="name">
<br>
You Email:
<input name="email" type="text" id="email">
</p>
<p>Sender Email:
<input name="sender_email" type="text" id="sender_email">
<br>
Comments<br>
<textarea name="comments" cols="50" rows="10" id="comments"></textarea>
</p>
<p>
<input name="submit" type="submit" id="submit" value="Submit">
</p>
</form>
Now we will write code
<?php
$name=$_POST['name'];
$email=$_POST['email']
$sender_email=$_POST['sender_email']
$comments=$_POST['comments']
//if name is empty then display error message "please fill out your name"
if (empty($name)){
$fname = NULL;
echo "Please fill out your name.<br />";
}
//if email is empty then display error message "please fill out your email "
if !empty($email)){
$email = NULL;
echo "Please fill out your name..<br />";
}
//if sender email is empty then display error message "please fill out sender email"
if (empty($sender_email)){
$sender_email = NULL;
echo "Please fill out sender email address.<br />";
}
//if sender comments field is empty then display error message "You forgot to leave a comment"
if (empty($comments')){
$comments = NULL;
echo "You forgot to leave a comment.<br />";
}
//if all fields are not empty then submit form
else{
$recipient = "yourname@yourdomain.com";
$subject = "Form Feedback";
$mailheaders = "Reply-to: $_POST[email]";
mail($recipient, $subject, $comments, $mailheaders);
}
$name=$_POST['name'];
$email=$_POST['email']
$sender_email=$_POST['sender_email']
$comments=$_POST['comments']
//if name is empty then display error message "please fill out your name"
if (empty($name)){
$fname = NULL;
echo "Please fill out your name.<br />";
}
//if email is empty then display error message "please fill out your email "
if !empty($email)){
$email = NULL;
echo "Please fill out your name..<br />";
}
//if sender email is empty then display error message "please fill out sender email"
if (empty($sender_email)){
$sender_email = NULL;
echo "Please fill out sender email address.<br />";
}
//if sender comments field is empty then display error message "You forgot to leave a comment"
if (empty($comments')){
$comments = NULL;
echo "You forgot to leave a comment.<br />";
}
//if all fields are not empty then submit form
else{
$recipient = "yourname@yourdomain.com";
$subject = "Form Feedback";
$mailheaders = "Reply-to: $_POST[email]";
mail($recipient, $subject, $comments, $mailheaders);
}
