Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Web Page Validation in PHP

 Validation means check the input submitted by the user.


 There are two types of validation are available in PHP.

 Client-Side Validation − Validation is performed on the client


machine web browsers.
 Server Side Validation − After submitted by data, The data has sent
to a server and perform validation checks in server machine

 Some of Validation rules for field

Field Validation Rules


Should required letters and white-
Name
spaces
Email Should required @ and .
Website Should required a valid URL
Radio Must be selectable at least once
Check Box Must be checkable at least once
Drop Down menu Must be selectable at least once

Example:

<?php
$name =""; // Sender Name
$email =""; // Sender's email ID
$message =""; // Sender's Message
$nameError ="";
$emailError ="";
$techError ="";
$messageError ="";
$errors=0 ;
$msg="";
if(isset($_POST['submit']))
{ // Checking null values in message.

$name = $_POST["name"]; // Sender Name


$email = $_POST["email"]; // Sender's email ID
$message = $_POST["message"]; // Sender's Message
$technologies = $_POST["radio1"];
if(isset($_POST['radio1']))
{
$technologies = $_POST["radio1"];
}
else
{
$technologies ="";
}
if (empty($_POST["name"]))
{
$nameError = "Name is required";
$errors = 1;
}
if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
{
$emailError = "Email is not valid";
$errors = 1;
}
if (!isset($_POST["radio1"])){
$techError = "Select one technology";
$errors = 1;
}
if (empty($_POST["message"])){
$messageError = "Message is required";
$errors = 1;
}
else
{
$msg ="Form Submitted successfully..."; // IF no errors
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>PHP Contact Form with Validation</title>
<style>
.error
{
color:red;
}
</style>
</head>
<body>
<div >
<div >
<h2>PHP Contact Form with Validation</h2>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>Name :</label>
<input class="input" type="text" name="name" value="">
<span class="error"><?php echo $nameError;?></span><br>
<label>Email :</label>
<input class="input" type="text" name="email" value="">
<span class="error"><?php echo $emailError;?></span><br>
<label>Courses :</label>
<div>
<input type="radio" name="radio1" value="PHP" > PHP
<input type="radio" name="radio1" value="HTML" > HTML
<input type="radio" name="radio1" value="PYTHON" > Python
</div>
<span class="error"><?php echo $techError;?></span><br>
<label>Message :</label>
<textarea name="message" value=""></textarea>
<span class="error"><?php echo $messageError;?></span>
<input class="submit" type="submit" name="submit"
value="Submit">
<div style="background-color:green"><?php echo $msg;?></div>
</form>
</div>
</div>
</body>
</html>

You might also like