Learn to get HTML form data using PHP

In this tutorial, we will learn about how to get and display HTML form data using PHP.

This form containing basic input fields but is necessary and important for any project's forms, we need to get the value of the text, radio, checkbox, select, text area input fields value.

<!DOCTYPE html>
<html>
<head>
 <title>PHP form</title>
</head>
<body>
<?php 
if(isset($_POST['register'])){     
// this isset() line of code is checking for user has clicked on submit button or not if yes then rest of code will run

 if(empty($_POST['name'])){     
// empty => this line of code checking name input field is empty or not using PHP empty() function, if yes it will display error message name is required

  echo "Name is required.";
  exit(); 
 // this exit() function will terminate execution of rest of program

 }


 if(! isset($_POST['gender'])){
// again this line of code checking gender radio input field is checked or not

  echo "Gender is required.";
  exit();
 }


 if(! isset($_POST['hobby'])){
// this line of code also checking for hobby check box input field is checked or not, if not checked atleast one will display error message.

  echo "Hobby is required.";
  exit();
 }
 if(! isset($_POST['subject'])){
// this line of code is checking that subject select field is selected or not, if one of the value is not selected it will display an error message.

  echo "Subject is required.";
  exit();
 }


 if(! empty($_POST['message'])){
// this line of code is checking that message textarea input field is empty or not if empty it will display an error message.

  echo "Message is required.";
  exit();
 }
// next five line of code are getting value from input fields.

 $name=$_POST['name'];
// this line of code is getting value of text input field

 $gender=$_POST['gender'];
// this line of code is getting value of radio input field.

 $hobby=$_POST['hobby'];
// this line of code is getting value of checkbox input field.

 $subject=$_POST['subject'];
// this line of code is getting value of select input field.

 $message=$_POST['message'];
// this line of code is getting value of message textarea input field.

 $data="";
// this is empty data variable, it is declared empty because in the next line of code we will concat some message in it.
// in the next line of code we are displaying variables value with message.

 $data.=" Your details is: <br>
 Name: $name <br>
 Gender: $gender <br>";
 $data.="Hobby:".implode(",", $hobby)."<br>";
 $data.="Subject: $subject <br>";
 $data.="Message: $message.<br>";
 echo $data;
// this echo statement displaying $data value as output.

}
?>

<!-- here we are creating a basic html form which contain number of form input fields. -->
<fieldset>
 <legend>PHP form</legend>
 <form action="data.php" method="post">
  <p>
   <label for="name">Name</label>
   <input type="text" name="name" id="name">
  </p>
  <p>
   <label for="gender">Gender</label>
   <input type="radio" name="gender" value="Male" id="gender">Male
   <input type="radio" name="gender" value="Female" id="gender">Female
<!-- here we don't need to include square bracket[] in the name attribute because number of radio buttong does not affect on getting radio button value because in number of radio button value we will be get of one selected radio button. -->

  </p>
  <p>
   <label for="hobby">Hobby</label>
   <input type="checkbox" name="hobby[]" value="Reading" id="hobby">Reading
   <input type="checkbox" name="hobby[]" value="Singing" id="hobby">Singing
<!-- Do not forget to include square bracket[] in the name attributes of checkbox beacause here user can select number of checkbox at a time. -->

  </p>

  <p>
   <label for="subject">Subject</label>
   <select name="subject" id="subject">
    <option value="">--Select subject--</option>
    <option value="Computer">Computer</option>
    <option value="Science">Science</option>
    <option value="Math">Math</option>
   </select>
  </p>

  <p>
   <label for="message">Message</label>
   <textarea name="message" id="message" cols="30" rows="10"></textarea>
  </p>
  <p>
   <input type="submit" name="register" value="Register">
  </p>
 </form>
</fieldset>
</body>
</html>

Post written by: Hemant Singh
© 2020 - 2020 All rights are reserved