PHP Form Validation - Required Fields
- Previous Page PHP Form Validation
- Next Page PHP Form URL/E-mail
This section shows how to create required input fields and create error messages as needed.
PHP - Input Fields
From the validation rules in the previous section, we see that the 'Name', 'E-mail', and 'Gender' fields are required. These fields cannot be empty and must be filled in in the HTML form.
Field | Validation Rules |
---|---|
Name | Required. It must contain letters and spaces. |
Required. It must contain a valid email address (including @ and .). | |
Website | Optional. If filled in, it must contain a valid URL. |
Comment | Optional. Multi-line input field (text box). |
Gender | Required. You must select an option. |
In the previous section, all input fields were optional.
In the following code, we have added some new variables: $nameErr, $emailErr, $genderErr, and $websiteErr. These error variables will store the error messages for the requested fields. We have also added an if else statement for each $_POST variable. This statement checks if the $_POST variable is empty (using the PHP empty() function). If it is empty, the error message will be stored in different error variables. If it is not empty, the user input data will be sent through the test_input() function:
<?php}} // Define variables and set them to empty values $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; } $name = test_input($_POST["name"]); } if (empty($_POST["email"])) { $emailErr = "Email is required"; } $email = test_input($_POST["email"]); } if (empty($_POST["website"])) { $website = ""; } $website = test_input($_POST["website"]); } if (empty($_POST["comment"])) { $comment = ""; } $comment = test_input($_POST["comment"]); } if (empty($_POST["gender"])) { $genderErr = "Gender is required"; } $gender = test_input($_POST["gender"]); } } ?>
PHP - Displaying Error Messages
In HTML forms, we add a little script after each requested field. If necessary, appropriate error messages are generated (if the user tries to submit the form without filling in required fields):
Example
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Name: <input type="text" name="name"> <span class="error">* <?php echo $nameErr;?></span> <br><br> E-mail: <input type="text" name="email"> <span class="error">* <?php echo $emailErr;?></span> <br><br> Website: <input type="text" name="website"> <span class="error"><?php echo $websiteErr;?></span> <br><br> <label>Comment: <textarea name="comment" rows="5" cols="40"></textarea> <br><br> Gender: <input type="radio" name="gender" value="female">Female <input type="radio" name="gender" value="male">Male <span class="error">* <?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"> </form>
The next step is to validate the input data, namely 'Is the 'Name' field only letters and spaces?', and 'Does the 'E-mail' field contain a valid email address syntax?' And if the 'Website' field is filled in, 'Does this field contain a valid URL?'.
- Previous Page PHP Form Validation
- Next Page PHP Form URL/E-mail