Form validation and Registration using PHP MySQL

Part 1: Form Validation

A simple and basic Registration form with necessary(limited) validations. Developed using HTML,PHP and MySQL. PHP used for validating and processing/storing user entered data. Tools used for this project are notepad and WAMP.
Initialy, a simple registration form with five fields is created using HTML <form> tag. See the image of the Registration form.
HTML code:
 <html> 
 <head><title>Registration Form</title></head> 
 <body> 
 <table align='center' border='0'> 
 <caption><h2>Registration Form</h2></caption> 
 <form method="post" action="#"> 
 <tr><td>FullName</td><td><input type="text" name="fname"/></td></tr> 
 <tr><td>Gender:
 </td><td>Female<input type="radio" value="female" name="gender"/> 
 Male<input type="radio" value="male" name="gender"/></td></tr 
 <tr><td>Email</td><td><input type="text" name="mail"/></td></tr> 
 <tr><td>Password</td><td><input type="password" name="pswd"/></td></tr> 
 <tr><td>Terms & condition</td><td><input type="checkbox" name="tc" value="agree"/></td></tr> 
 <tr colspan='1'><td></td><td><input type="submit" name="btnsubmit"value="SUBMIT"/> 
 <input type="Reset" name="btnreset" value="Reset"/></td></tr></form> 
 </table> 
 </body> 
 </html> 
Now, let's see how to validate the above form. PHP is a server-side scripting language. PHP mainly used for processing data from the client machine. In order to process the data and to get the correct result after processing, their required some validation or checking the data from the client. HTML( web language) is static in nature, means just show the page. It doesnot do any processing. But, there are options to make this static page a dynamic one. For client-side validation mainley used scripting language is JavaScript, and for Server-side processing PHP is used.Both scripting language helps HTML page interactive and dynamic. Here, in this project form validation and form processing/storing are implemented by PHP. Below code show how each form field is checked using PHP. (Include this PHP code in header part of the previousely created form).
PHP validation:
 <html> 
 <head> 
 <?php 
 $fnameer=$gener=$mailerr=$pswder=$tcer=""; //variables to hold the warning message
 $fname=$gender=$addr=$mail=$pswd=$tc=$num=$nat=""; 
 $v=0; 
 if($_SERVER['REQUEST_METHOD']=='POST'){ 
 $v=true; 
 if(empty($_POST["fname"])){ 
 $fnameer="First Name required";$v=false; 
 }else{ 
 $fname=fun_input($_POST["fname"]); 
 if(!preg_match("/^[a-zA-Z ]*$/",$fname)){ 
 $fnameer="only characters and whitespace allowed";$v=0; 
 }else{$v=true;} 
 } 
 if(empty($_POST["gender"])){ 
 $gener="select an option";$v=false; 
 }else{$v=true; 
 $gender=fun_input($_POST["gender"]); 
 } 
 if(empty($_POST["mail"])){ 
 $mailerr="provide mail ID";$v=0; 
 }else{ 
 $mail=fun_input($_POST["mail"]); 
 if(!filter_var($mail,FILTER_VALIDATE_EMAIL)){ 
 $mailerr="please provide the valid characters";$v=0; 
 } 
 } 
 if(empty($_POST["pswd"])){$pswder="password required"; 
 }else{$v=true; 
 $pswd=fun_input($_POST["pswd"]); 
 } 
 if(empty($_POST["tc"])){ 
 $tcer="please accept the terms & condetions";$v=0; 
 }else{$v=true; 
 $tc=fun_input($_POST["tc"]); 
 } 
 } 
 function fun_input($vals){ 
 $vals=trim($vals); 
 $vals=stripslashes($vals); 
 $vals=htmlspecialchars($vals); 
 return $vals; 
 } 
 if($v){ 
 session_start();//d proccessing start here 
 $_SESSION['fname']=$fname; 
 $_SESSION['pswd']=$pswd; 
 $_SESSION['mail']=$mail; 
 $_SESSION['gender']=$gender; 
 header('location:reg_db.php'); 
 } 
 ?> 
<title>Registration Form</title>
</head>
As you alredy seen that this form has 5 input field. Each one of them should be checked by the php in the header part. If any field has left blank then form data not submitted for proccessing/storing. Inorder to check the fields in the form before submitting to server there require some modificaton in form. The modified HTML form is shown below:
 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
 <tr><td>FullName</td><td><input type="text" name="fname"/></td></tr> 
 <tr><td>Gender: 
 </td><td>Female<input type="radio" value="female" name="gender"/> 
 Male<input type="radio" value="male" name="gender"/></td></tr 
 <tr><td>Email</td><td><input type="text" name="mail"/></td></tr> 
 <tr><td>Password</td><td><input type="password" name="pswd"/></td></tr> 
 <tr><td>Terms & condition</td><td><input type="checkbox" name="tc" value="agree"/></td></tr> 
 <tr colspan='1'><td></td><td><input type="submit" name="btnsubmit"value="SUBMIT"/> 
 <input type="Reset" name="btnreset" value="Reset"/></td></tr> 
 </form> 
After the modification you can see the ‘action’ attribute of <form>tag has changed, action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>". Here, the value of action attribute provided with a php function, htmlspecialchars(), whcich replaces/converts some spacial predefined characters to HTML entites with &(ampersand) as escape sequence and $_SERVER["PHP_SELF"]states that form should be handled by same URL instead jumping or redirecting to other page.
We have provided the necessary actions. As the php process each field in the form, if user left any field blank or did any invalid entry then form will show the warning message. For this part we need to fetch the variable which holds the warning message to the form. This done with adding a small php code to each field of form. Check the below shown code for clarification.
 <title>RegistrationForm</title> 
 <style>.error{color:red;}</style><!-- for showing warnig message red colored--> 
 </head> 
 <body> 
 <table align='center' border='0'> 
 <caption><h2>Registration Form</h2></caption> 
 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
 <tr><td>FullName</td><td><input type="text" name="fname"/> <tt class="error"><?php echo $fnameer;?></tt></td></tr> <!--php warning message code-->
 <tr><td>Gender: 
 </td><td>Female<input type="radio" value="female" name="gender"/> 
 Male<input type="radio" value="male" name="gender"/><?php echo $gener;?></td></tr 
 <tr><td>Email</td><td><input type="text" name="mail"/><tt class="error"><?php echo $mailerr;?></tt></td></tr> 
 <tr><td>Password</td><td><input type="password" name="pswd"/><tt class="error"><?php echo $pswder;?></tt></td></tr> 
 <tr><td>Terms & condition</td><td><input type="checkbox" name="tc" value="agree"/> 
 <tt class="error"><?php echo $tcer?></tt></td></tr> 
 <tr colspan='1'><td></td><td><input type="submit" name="btnsubmit"value="SUBMIT"/> 
 <input type="Reset" name="btnreset" value="Reset"/></td></tr></form></table> 
 </body> 
 </html> 
When user clicks the submit button without filling the form then output will be as shown in the picture below:
So, the form validation part concluded here. Now, consider the user fill all the field correctly and click the Submit button. All the form data will transfer to Server and store in the database, and redirect the user to greeting page. All these process are implemented by PHP with MySQL database. For this, first create a ...Click here to read the rest

-- Thank you --


Please don't leave without commenting ...

1 comment:

Aceproschool said...

First time to your blog, this article is excellent. You have chosen every important detail of PHP. I have learned many new things from this blog. PHP Mysql is very important thing in this era. Thanks for sharing this post. Keep posting.