PHP

HTML Form Processing in PHP

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'])){
	if(!isset($_POST['fname']) || !isset($_POST['lname']) || !isset($_POST['r']) || !isset($_POST['qual'])){
	$error="Required";
	echo $error.' Fields Left Blank!';
   }
   else
   {
	$fn=$_POST['fname'];
	$ln=$_POST['lname'];
	$gn=$_POST['r'];
	$qual=$_POST['qual'];
	$sk=$_POST['c'];
 
        if($gn=='m')
		$g='Male';
	else
		$g='Female';

	if($qual=='g')
		$q='Graduate';
	elseif($qual=='p')
		$q='Post Graduate';
	else
		$q='Other';

	$str='';
	if(!empty($_POST['c'])){
        foreach($_POST['c'] as $s)
	{
		
			$str=$str.' '.$s;
	}}
   }
 }
?>
<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
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

You may also like...