php form validation
Hi Friends today we will learn how validation form in php..in this lesson you will learn empty fields validation,email validation,confirm password validation and more..
php form validation |
below php codes use to validate form..
<?php // initializing variables //We use array_push to store all errors into $errors variable $errors = array(); $conn=new mysqli('localhost','root','','test'); if(isset($_POST['submit'])){ //UserName Checking if(empty($_POST['name'])){ $nameerror='Your UserName is Empty'; array_push($errors,'UserName'); } //Email Checking and Validation Start if(!empty($_POST['email'])){ if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){ $emailerror='<font color=green>'.$_POST['email'].'</font> is Not Valid'; array_push($errors,'email not Valid'); } }else{ $emailerror='Your Email is Empty'; array_push($errors,'email empty'); } //Email Validation End //Password Validation if(!empty($_POST['password'])){ if(strlen($_POST['password'])<8){ $passerror='Your Password Should not be less Than 8 Characters'; array_push($errors,'password error'); } }else{ $passerror='Your Password is Empty'; array_push($errors,'password empty'); } //Confirm Password Validation if($_POST['password']!==$_POST['passconf']){ $passconferror='Your Password Did Not Match'; array_push($errors,'password not matched'); } //Full Name Validation if(empty($_POST['fname'])){ $fnameerror="Your Full Name Can Not be Empty"; array_push($errors,'Full Name empty'); } //Sex Checking if(empty($_POST['sex'])){ $sexerror="Your Sex Can Not be Empty"; array_push($errors,'Sex empty'); } //Country Checking if(empty($_POST['country'])){ $countryerror="Your Country Can Not be Empty"; array_push($errors,'Country empty'); } //if Errors count to 0 ..means no errors occured if(count($errors)==0){ //escaping from string $username=mysqli_real_escape_string($conn,$_POST['name']); $password=mysqli_real_escape_string($conn,$_POST['password']); $passconf=mysqli_real_escape_string($conn,$_POST['passconf']); $email=mysqli_real_escape_string($conn,$_POST['email']); $fname=mysqli_real_escape_string($conn,$_POST['fname']); $sex=mysqli_real_escape_string($conn,$_POST['sex']); $country=mysqli_real_escape_string($conn,$_POST['country']); //Now you can insert data into database $success= 'Successfully Validate'; }else{ $error='Fill The Registration form Correctly'; } } ?>
HTML Form:
<div align="center"> <h3>PHP Form Validation</h3> <form method="POST"> <table width="50%"> <tr> <td>UserName:</td> <td><input type="text" name="name"/> <div><?php if(isset($nameerror)){ echo '<div class=error>'.$nameerror.'</div>';}?></div> </td> </tr> <tr> <td>Email:</td> <td><input type="text" name="email"/> <div><?php if(isset($emailerror)){ echo '<div class=error>'.$emailerror.'</div>';}?></div> </td> </tr> <tr> <td>Password:</td> <td><input type="text" name="password"/> <div><?php if(isset($passerror)){ echo '<div class=error>'.$passerror.'</div>';}?></div> </td> </tr> <tr> <td>Confirm Password:</td> <td><input type="text" name="passconf"/> <div><?php if(isset($passconferror)){ echo '<div class=error>'.$passconferror.'</div>';}?></div> </td> </tr> <tr> <td>Full Name:</td> <td><input type="text" name="fname"/> <div><?php if(isset($fnameerror)){ echo '<div class=error>'.$fnameerror.'</div>';}?></div> </td> </tr> <tr> <td>Sex:</td> <td><input type="radio" name="sex" value="female"/>Female <input type="radio" name="sex" value="male"/> Male <input type="radio" name="sex" value="other"/>Other <div><?php if(isset($sexerror)){ echo '<div class=error>'.$sexerror.'</div>';}?></div> </td> </tr> <tr> <td>Country:</td> <td> <select type="text" name="country"> <option value="">Select Country</option> <option value="india">India</option> <option value="usa">United States</option> <option value="japan">Japan</option> <option value="russia">Russia</option> </select> <div><?php if(isset($countryerror)){ echo '<div class=error>'.$countryerror.'</div>';}?></div> </td> </tr> <tr> <td></td> <td><input type="submit" name="submit"/></td> </tr> </table> </form> <?php if(isset($success)){ echo $success;}?> <?php if(isset($error)){ echo $error;}?> </div>
Complete PHP form Validation: validation.php
<?php // initializing variables //We use array_push to store all errors into $errors variable $errors = array(); $conn=new mysqli('localhost','root','','test'); if(isset($_POST['submit'])){ //UserName Checking if(empty($_POST['name'])){ $nameerror='Your UserName is Empty'; array_push($errors,'UserName'); } //Email Checking and Validation Start if(!empty($_POST['email'])){ if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){ $emailerror='<font color=green>'.$_POST['email'].'</font> is Not Valid'; array_push($errors,'email not Valid'); } }else{ $emailerror='Your Email is Empty'; array_push($errors,'email empty'); } //Email Validation End //Password Validation if(!empty($_POST['password'])){ if(strlen($_POST['password'])<8){ $passerror='Your Password Should not be less Than 8 Characters'; array_push($errors,'password error'); } }else{ $passerror='Your Password is Empty'; array_push($errors,'password empty'); } //Confirm Password Validation if($_POST['password']!==$_POST['passconf']){ $passconferror='Your Password Did Not Match'; array_push($errors,'password not matched'); } //Full Name Validation if(empty($_POST['fname'])){ $fnameerror="Your Full Name Can Not be Empty"; array_push($errors,'Full Name empty'); } //Sex Checking if(empty($_POST['sex'])){ $sexerror="Your Sex Can Not be Empty"; array_push($errors,'Sex empty'); } //Country Checking if(empty($_POST['country'])){ $countryerror="Your Country Can Not be Empty"; array_push($errors,'Country empty'); } //if Errors count to 0 ..means no errors occured if(count($errors)==0){ $username=mysqli_real_escape_string($conn,$_POST['name']); $password=mysqli_real_escape_string($conn,$_POST['password']); $passconf=mysqli_real_escape_string($conn,$_POST['passconf']); $email=mysqli_real_escape_string($conn,$_POST['email']); $fname=mysqli_real_escape_string($conn,$_POST['fname']); $sex=mysqli_real_escape_string($conn,$_POST['sex']); $country=mysqli_real_escape_string($conn,$_POST['country']); $success= 'Successfully Validate'; }else{ $error='Fill The Registration form Correctly'; } } ?> <style> .error{ color:red; font-weight:bold; font-style:italic; } input[type=text]{ padding:5px; border-radius:4px; } input[type=submit]{ padding:7px; background:black; color:white; border-radius:4px; cursor:pointer; } input[type='radio']:after { width: 15px; height: 15px; border-radius: 15px; top: -2px; left: -1px; position: relative; background-color: #d1d3d1; content: ''; display: inline-block; visibility: visible; border: 2px solid white; } input[type='radio']:checked:after { width: 15px; height: 15px; border-radius: 15px; top: -2px; left: -1px; position: relative; background-color: #ffa500; content: ''; display: inline-block; visibility: visible; border: 2px solid white; } select { padding:3px; margin: 0; font-size:15px; width:185px; border-radius:4px; background: white; color:#888; -webkit-appearance:none; -moz-appearance:none; appearance:none; cursor:pointer; } </style> <div align="center"> <h3>PHP Form Validation</h3> <form method="POST"> <table width="50%"> <tr> <td>UserName:</td> <td><input type="text" name="name"/> <div><?php if(isset($nameerror)){ echo '<div class=error>'.$nameerror.'</div>';}?></div> </td> </tr> <tr> <td>Email:</td> <td><input type="text" name="email"/> <div><?php if(isset($emailerror)){ echo '<div class=error>'.$emailerror.'</div>';}?></div> </td> </tr> <tr> <td>Password:</td> <td><input type="text" name="password"/> <div><?php if(isset($passerror)){ echo '<div class=error>'.$passerror.'</div>';}?></div> </td> </tr> <tr> <td>Confirm Password:</td> <td><input type="text" name="passconf"/> <div><?php if(isset($passconferror)){ echo '<div class=error>'.$passconferror.'</div>';}?></div> </td> </tr> <tr> <td>Full Name:</td> <td><input type="text" name="fname"/> <div><?php if(isset($fnameerror)){ echo '<div class=error>'.$fnameerror.'</div>';}?></div> </td> </tr> <tr> <td>Sex:</td> <td><input type="radio" name="sex" value="female"/>Female <input type="radio" name="sex" value="male"/> Male <input type="radio" name="sex" value="other"/>Other <div><?php if(isset($sexerror)){ echo '<div class=error>'.$sexerror.'</div>';}?></div> </td> </tr> <tr> <td>Country:</td> <td> <select type="text" name="country"> <option value="">Select Country</option> <option value="india">India</option> <option value="usa">United States</option> <option value="japan">Japan</option> <option value="russia">Russia</option> </select> <div><?php if(isset($countryerror)){ echo '<div class=error>'.$countryerror.'</div>';}?></div> </td> </tr> <tr> <td></td> <td><input type="submit" name="submit"/></td> </tr> </table> </form> <?php if(isset($success)){ echo $success;}?> <?php if(isset($error)){ echo $error;}?> </div>
That's It friends all about php form validation..
Thank you for Visiting ...
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.