Search code examples
issetrequired

PHP form handler-using "required" instead "isset" when not filled


Is it good to use "required" in input like this:

Name:<input type="text" name="name" required>

Or should I remove it and use "isset" in PHP? If I understand properly, both show you error, when you want to proceed next without filling input's, but required just awares you, while isset throw's you mostly to new page and reset the form(not good if you only forgot to fill one input).


Solution

  • Or should I remove it and use "isset" in PHP?

    Why do you feel the two are mutually exclusive? Use both.

    The required HTML attribute provides a smoother client-side user experience. It allows some basic validation without having to re-engage the server. It does not, however, do anything to validate incoming requests in server-side code. isset (among lots of other potential logic) can and should be used server-side to do just that.

    Use client-side validation logic to present users with a better user experience. Use server-side validation logic to actually validate the user input before conducting business logic. Validation can live in lots of places for lots of reasons.