PHP 5 Forms - Required Fields

This section tells the best way to make input fields required and make blunder messages if needed.

PHP - Required Fields

From the approval rules table on the past page, we see that the "Name", "E-mail", and "Gender" fields are required. These fields can't be vacant and must be rounded out in the HTML form.

Field Validation Rules
Name Required. + Must just contain letters and whitespace
E-mail Required. + Must contain a legitimate email address (with @ and .)
Website Optional. On the off chance that present, it must contain a substantial URL
Comment Optional. Multi-line input field (textarea)
Gender Required. Must choose one

In the past part, all info fields were discretionary.

In the accompanying code we have included some new factors: $nameErr, $emailErr, $genderErr, and $websiteErr. These mistake factors will hold blunder messages for the required fields. We have likewise included an if else proclamation for each $_POST variable. This checks if the $_POST variable is unfilled (with the PHP vacant() work). In the event that it is unfilled, a blunder message is put away in the diverse mistake factors, what's more, on the off chance that it isn't unfilled, it sends the client input information through the test_input() function:

//characterize factors and set to exhaust values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

  on the off chance that (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);

  if (empty($_POST["website"])) {
    $website = "";
  } else {
    $website = test_input($_POST["website"]);

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

  if (empty($_POST["gender"])) {
    $genderErr = "Gender is required";
  } else {
    $gender = test_input($_POST["gender"]);

PHP - Display The Error Messages

Then in the HTML structure, we include a little content after each required field, which produces the right blunder message if necessary (that is if the client endeavors to submit the structure without rounding out the required fields):


<form method="post" action="<?php reverberation htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Name: <input type="text" name="name">
<span class="error">* <?php reverberation $nameErr;?></span>
<input type="text" name="email">
<span class="error">* <?php reverberation $emailErr;?></span>
<input type="text" name="website">
<span class="error"><?php reverberation $websiteErr;?></span>
Remark: <textarea name="comment" rows="5" cols="40"></textarea>
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php reverberation $genderErr;?></span>
<input type="submit" name="submit" value="Submit">

Run model »

The subsequent stage is to approve the information, that is "Does the Name field contain just letters and whitespace?", and "Does the E-mail field contain a legitimate email address syntax?", and whenever rounded out, "Does the Website field contain a substantial URL?".