Google+ PHP TUTORIALS ONLINE HELP FOR MEDICAL AND PROGRAMMING: PHP form Validation

Monday 17 February 2014

PHP form Validation

PHP form Validation

We are going to discus how to use PHP Form Validation. Now First off all we have display HTML form with meny text fields and submit button First name, Lastname,MobileNo,PhoneNo Email, Address like so on In this example we have used many method and function. First thing we had done is to pass all variables through PHP's htmlspecalchars() function. After the we fills all input field than ,we click submit button and display all input fields values. Now we have used $_SERVER[REQUEST_METHOD]. If the REQUEST_METHOD is POST, and submitted form. If user not submit  all input fields than show for error massage please submit all  required fields and after that user click on the submit button than display all input fields
We have use for if and else statements
Example
<!DOCTYPE HTML> 
<html>
<head>
<style>
.error {color: #FF0000;}
body {
background-color: #D1D1D1;
}
</style>
<meta charset="utf-8">
</head>
<body> 

<?php
// Define variables and set to empty values:
$nameErr =$lnameErr = $emailErr = $mobilenoErr = $phonenoErr = $genderErr = $addressErr = "";
$name = $lname= $email = $mobileno = $gender = $address = $phoneno = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{

if (empty($_POST["name"]))
{$nameErr = "Name is required";}
else
{$name = test_input($_POST["name"]);}

if (empty($_POST["lname"]))
{$lnameErr = "Last Name is required";}
else
{$lname = test_input($_POST["lname"]);}

if (empty($_POST["email"]))
{$emailErr = "Email is required";}
else
{$email = test_input($_POST["email"]);}

if (empty($_POST["mobileno"]))
{$mobilenoErr = "Mobile No is required";}
else
{$mobileno = test_input($_POST["mobileno"]);}

if (empty($_POST["phoneno"]))
{$phonenoErr = "Phone No No is required";}
else
{$phoneno = test_input($_POST["phoneno"]);}



if (empty($_POST["address"]))
{$addressErr = "Address is required";}
else
{$address = test_input($_POST["address"]);}

if (empty($_POST["gender"]))
{$genderErr = "Gender is required";}
else
{$gender = test_input($_POST["gender"]);}

}

function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<h2>PHP Form Validation Example</h2>

<table>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
<tr>
<td>Name:</td>
<td> <input type="text" name="name"> <span class="error">* <?php echo $nameErr;?></span></td>

</tr>
<tr>
<td>Last Name:</td>
<td> <input type="text" name="lname"> <span class="error">* <?php echo $lnameErr;?></span></td>

</tr>

<tr>
<td> E-mail:</td>
<td> <input type="text" name="email"> <span class="error">* <?php echo $emailErr;?></span></td>
</tr> 

<tr>
<td> Mobile NO</td>
<td><input type="text" name="mobileno"> <span class="error">* <?php echo $mobilenoErr;?></span></td>
</tr> 

<tr>
<td>Phone No</td>
<td><input type="text" name="phoneno"> <span class="error">* <?php echo $phonenoErr;?></span></td>
</tr> 
<tr>
<td>Address:</td>
<td><textarea name="address" rows="5" cols="20"></textarea> <span class="error">* <?php echo $addressErr;?></span></td>
</tr> 
<tr>
<td> Gender:</td>
<td><input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $genderErr;?></span></td>
</tr> 

<tr>
<td>&nbsp;</td>
<td> <input type="submit" name="submit" value="Submit"> </td>
</tr>
</form>
</table>


<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $lname;
echo "<br>";
echo $email;
echo "<br>";
echo $mobileno;
echo "<br>";
echo $phoneno;
echo "<br>";
echo $address;
echo "<br>";
echo $gender;
?>

</body>
</html>

Output



No comments:

Post a Comment