PHP Form Required

PHP required fields means the input fields in a form must be filled if users wish to proceed with the form submission process.

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

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 PHP form.

Must Read: PHP Form Validation

PHP Required Fields

Using PHP forms, we can set certain fields to be required and display error messages. It is essential when 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 below block of code, we added some new variables $name_error, $email_error, $gender_error, and $website_error.We will use them to display errors these variables will hold error 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 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>

Conclusion

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

I hope you have a better understanding of PHP Form Required Fields. 🙂

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 part of web development in PHP. There are lots of vulnerabilities and exploits...

Close