PHP Form Required

PHP required fields mean the input fields in a form must be filled if the user wishes to proceed with the form submission process.

An asterisk (*) sign is useful to mark the required fields.4

PHP required fields are also programmed to display error messages when left empty.

In this unit, we will learn how to check Required Fields in a PHP form

Must Read: PHP Form Validation

PHP Required Fields

Using PHP forms, we can set certain fields as mandatory and display error messages, in case the user’s input doesn’t comply with the parameters set in the web form. It is essential for making any form for actual use with databases and validation.

These fields cannot be empty and must be filled out in the HTML form.

FieldValidation Rules
Name (Required)Must only contain letters and whitespace
Email (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 (text area)
Gender (Required)Must select one

In the given block of code, we added some new variables: $name_error, $email_error, $gender_error, and $website_error.We will use them to display errors messages in case required fields are left empty.

It can be achieved by using if-else conditionals for every $_POST variable—these statements in PHP check if a variable is empty using the empty() function. In case if it is empty, we store an error message inside the error variable.

Example

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $name_error = "Required";
    } else {
        $fullname = proc_input($_POST["name"]);
    }

    if (empty($_POST["email"])) {
        $email_error = "Required";
    } else {
        $email = proc_input($_POST["email"]);
    }

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

    if (empty($_POST["feedback"])) {
        $feedback = "";
    } else {
        $feedback = proc_input($_POST["feedback"]);
    }

    if (empty($_POST["gender"])) {
        $gender_error = "Required";
    } else {
        $gender = proc_input($_POST["gender"]);
    }
}
?>

Displaying Error Messages

We add a bit of code to each of the required field in the HTML code. To display the error messages.

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
    <input type="text" name="name" placeholder="name">
    <span>*<?php echo $name_error;?></span>
    <br>
    <input type="text" name="email" placeholder="E-mail">
    <span>*<?php echo $email_error;?></span>
    <br>
    <input type="text" name="website" placeholder="Website">
    <span><?php echo $website_error;?></span>
    <br>
    <textarea name="feedback" rows="6" cols="40" placeholder="Feedback"></textarea><br><br> 
    <input type="radio" name="gender" value="f">F
    <input type="radio" name="gender" value="m">M 
    <span>*<?php echo $gender_error;?></span>
    <br>
    <br>
    <input type="submit" name="submit" value="Submit"> 
</form>

Read Also: PHP Form URL/Email Validation

Conclusion

If we set certain fields in the forms as required and they are left empty, an error message will be displayed. Required fields ensure we get all the data we need from the users during registration.

We hope you have been able to understand PHP Form Required Fields. If you want to understand the other concepts of PHP programming, do refer to the PHP Tutorial and PHP MySQL sections. Happy Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
form validation in PHP
PHP Form Validation

Form validation is one of the major parts of web development in PHP. A PHP form is vulnerable to several...

Close