Jump to content

Reload form results if there is an error or invalidation


classic

Recommended Posts

I have seen this done before, but wondering if this is possible with straight PHP or if Javascript would be better served doing it. Basically, if there is a form validation issue or they forget to fill out a field, the form reloads with the values still in place as opposed to being cleared out and the user having to retype everything. What is the best way to go about that?

Link to comment
Share on other sites

The usual way is for the validation script to be located in the same document as the form, so that it is submitting to itself. something like. The PHP is at the top of the document. If the request method is GET, you set some variables to their default values. If the request method is POST, you do your validation. Failure would set those variables to different values.Embedded in the HTML section, you have some "mini-scripts" that use those variables in some way, either to add some text, or to make some text visible, or to change the class of some text so that it appears in a different color, or even to make images visible (like checks or other symbols).Obviously, if the method is POST and the data validates, the user does not see the form again. Success would trigger a location header and an exit statement.I hope all that makes sense.

Link to comment
Share on other sites

Oh, yes. All this can be done with JavaScript, usually by tweaking CSS values (display, color, etc.), as I described above. But you need the other technique also for those users with JavaScript disabled, and also because you should never rely on javascript as your only means of validation. It's great for the most responsive user interface. But you need the server-side stuff also, as a back-up, and also to protect against malicious users and hackers.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...