HTML Form Processing in PHP

0
24

In this article on HTML Form Processing in PHP, I will explain how to process the Form data in PHP.

Since, the HTML forms serve as means for gathering data from user, form processing is significant in any web application that allows users to interact. The following example explains how to retrieve user input from an HTML form and display the values ​​entered by the user. Furthermore, here we also check some of the fields in the form for blank values.

As can be seen in the example, here we use a single PHP file for displaying the form as well as retrieving the values. Therefore, we need to use the function isset () so that the processing occurs only when the user clicks on the Submit button. So, the first call to the isset () function checks whether the user has clicked on the submit button. Secondly, the next if statement checks whether the corresponding fields contain some value or not. In case, any of these fields is left blank, the variable $ error is set and the error message is displayed. Otherwise, we create different variables containing the values ​​from each of these form fields. Since, the checkboxes form an array of values, we need to use an array for them. Also, the function empty () determines whether the corresponding variable exists or not.

Further, when we display the table, first we need to check whether the submit button is clicked or not as well as there is no error.

The Complete Code for HTML Form Processing in PHP

<?php
 if(isset($_POST['submit']))
?>
<html>
 <head>
   <title>Form Processing</title>
 </head>
 <body>
   <form name="f1" method="POST">
	First Name: <input type="text" name="fname"/><br>
	Last Name: <input type="text" name="lname"/><br>
	Gender: <input type="radio" name="r" value="m"/> Male 
<input type="radio" name="r" value="f"/> Female<br>
	Skills: <input type="checkbox" name="c[]" value="PHP"/> PHP 
		<input type="checkbox" name="c[]" value="C"/> C 
		<input type="checkbox" name="c[]" value="Java"/> Java <br>
	Qualification: 
		<select name="qual">
                   <option value="g">Graduate</option>
		   <option value="p">Post Graduate</option>
		   <option value="o">Other</option>
		</select><br>
		<input type="submit" name="submit"/>     
   </form>
   <?php
      if(isset($_POST['submit']))
	if(!isset($error))
   ?>
   <table border="2">
     <tr>
	     <th>First Name</th>
	     <th>Last Name</th>
	     <th>Gender</th>
	     <th>Skills</th>
	     <th>Qualification</th>
     </tr>
		<td><?php echo $fn;?></td>
		<td><?php echo $ln;?></td>
		<td><?php echo $g;?></td>
		<td><?php echo $str;?></td>
		<td><?php echo $q;?></td>
     <tr>
     </tr>
   <?php
	
   ?>
 </body>
</html>

Output

Before Clicking on the Submit Button
The Output of the HTML Form Processing in PHP
The Output of the HTML Form Processing in PHP
Blank Field Error Check
Blank Field Error Check

Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here