logo
PHP Forms Required Fields
The PHP From  validation rules table on the previous page, we see that the "Name", "E-mail", and "Gender" fields are required. These fields cannot be empty and must be filled out in the HTML form.  PHP form required fields are explained in the following table :
Field Validation Rules
Name Required. Must only contain letters and whitespace
E-mail Required. Must contain a valid email address (with @ and .)
Website Optional. If present, it must contain a valid URL
Comment Optional. Multi-line input field (textarea)
Gender Required. Must select one

We have added some new variables in the following required fields code : $name_error, $email_error, $gender_error, and $website_error These error variables will hold error messages for the required fields. We have also added an if else statement for each $_POST variable. This checks if the $_POST variable is empty (with the PHP empty() function). If it is empty, an error message is stored in the different error variables, and if it is not empty, it sends the user input data through the my_form() function :

<?php
$name_error = $email_error = $gender_error = $website_error = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $name_error= "Name field is required";
  } else {
    $name = my_form($_POST["name"]);
  }
  
  if (empty($_POST["email"])) {
    $email_error = "Email field is required";
  } else {
    $email = my_form($_POST["email"]);
  }
    
  if (empty($_POST["website"])) {
    $website = "";
  } else {
    $website = my_form($_POST["website"]);
  }

  if (empty($_POST["comment"])) {
    $comment = "";
  } else {
    $comment = my_form($_POST["comment"]);
  }

  if (empty($_POST["gender"])) {
    $gender_error = "Gender field is required";
  } else {
    $gender = my_form($_POST["gender"]);
  }
}

function my_form($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
PHP Required Fields Example
<!DOCTYPE HTML>  
<html>
<head>
<style>
.error_message {
	color: #FF0000;
	font-size:16px;
	font-weight:bold;
	font-style:italic;
	}
.margin{ margin:5px 0px 10px; clear:both;}
.custom_form{ 
	padding:5px 10px; 
	border:#0099da 1px solid; 
	border-radius:2px;
}
.cus_btn{ background:#0099da; padding:6px 15px; border-radius:2px; color:#FFF;}
.cus_btn:hover{ background:#069;}
</style>
</head>

<body>  
<!-- Start PHP Code -->
<?php
$name_error = $email_error = $gender_error = $website_error = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $name_error= "Name field is required";
  } else {
    $name = my_form($_POST["name"]);
  }
  
  if (empty($_POST["email"])) {
    $email_error = "Email field is required";
  } else {
    $email = my_form($_POST["email"]);
  }
    
  if (empty($_POST["website"])) {
    $website = "";
  } else {
    $website = my_form($_POST["website"]);
  }

  if (empty($_POST["comment"])) {
    $comment = "";
  } else {
    $comment = my_form($_POST["comment"]);
  }

  if (empty($_POST["gender"])) {
    $gender_error = "Gender field is required";
  } else {
    $gender = my_form($_POST["gender"]);
  }
}

function my_form($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
<!-- End PHP Code -->

<p><div class="error_message">* Required field.</div></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  <div class="margin">
      Name: <input type="text" name="name" class="custom_form" placeholder="Your Name">
      <span class="error_message">* <?php echo $name_error;?></span>
  </div>
  <div class="margin">
  E-mail: <input type="email" name="email" class="custom_form" placeholder="example@gmail.com">
  <span class="error_message">* <?php echo $email_error;?></span>
  </div>
  <div class="margin">
  Website: <input type="text" name="website" class="custom_form" placeholder="http://www.sample.com">
  <span class="error_message"><?php echo $website_error;?></span>
  </div>
  <div class="margin">
  Comment: <textarea name="comment" class="custom_form" rows="5" cols="40" placeholder="Write message"></textarea>
  </div>
  <div class="margin">
  Gender:
  <input type="radio" name="gender" value="female">Female
  <input type="radio" name="gender" value="male">Male
  <span class="error_message">* <?php echo $gender_error;?></span>
  </div>
  <input type="submit" name="submit" class="cus_btn" value="SUBMIT">  
</form>

</body>
</html>