PHP

Marc Dimacuha
Marc Dimacuha
8,348 Points

Using "required" values in input instead

I'm just wondering if creating an if statement to the top checking the values are filled out will be the same thing as typing the 'required' field in <input>?

What's the difference between:

if ($name == "" || $email == "" || $details == "") {
        echo "Please fill in the required fields : 
                   Name, Email and Details";
        exit;
     }

and:

<input type="text" id="name" name="name" value="" required>

Will it have more security measures since we're doing more code or will it serve the same purpose with 'required'?

2 Answers

Pontus Bolmér
Pontus Bolmér
8,464 Points

It is the same as required. But if you are going to save the data in a database, there are some other checks that are a good thing to do, like htmlspecialchars.

I made a small contact form for a client, I don't find it necessary, but I think it certainly would be a smart move just to include a PHP check as well.