Processing Forms: Javascript or PHP?
PHP August 29, 2007

I've dealt with a lot of online forms. Most, on submit, will refresh the page and process the data before the page loads. Then it will tell you of any errors you have.

Sometimes, a page reload can take a while. If you're filling out a long form and then wait a minute for the page to reload, only to tell you that you forgot to fill in something, it can be quite frustrating.

Processing forms with Javascript FIRST can make all the difference, especially if you need to check for blanks before actually processing the information. You can easily write a Javascript function to run through all the form elements and check for blanks, returning any errors it finds. This happens lightning fast for the user, so there's no need to wait. THEN, if everything is set, you can either choose to refresh the page and process the information in PHP (forgetting about checking for blanks, since that's already been done) and that will also save some page reload time.

You can also choose to send all the form data via AJAX to a separate PHP page which will process it all and return something back. In this way, there is no need to reload the page and you can just display to the user a spinning gear while AJAX is retrieving results.

I think seeing a spinning gear is sometimes a lot nicer than seeing a blank page. Spinning gears tell you something is working at least. Imagine trying to search for airline tickets and seeing a blank page for 20 seconds while it works in the background. Not so hot.

But reloading a page to process a form has its advantages. I'd recommend you try both options to see which works best for your site. For example, logging in - which will most likely need to take you to a new page anyway - might be the kind of form you want to process by page refresh. Changing your information only to see a "success" message doesn't require a page refresh.

So feel it out, but keep the user's experience in mind.


Other PHP Posts

Feb 08, 2008

Always Coding Generically

I've been programming websites for a while now and though I often do things fast for prototyping purposes, I find myself wishing I coded in a more generic manner. Sure, it's easy to go ahead and write the code as you ...

Jan 14, 2008

Using PHP When Possible

In recent months, I've gotten in the habit of writing things in PHP arrays, using a function with a loop to actually generate HTML. This works great for anything where you have repeating elements (such as a table, lists,...

Jul 19, 2007

PHP Arrays

PHP hashmap-style arrays make a great solution to wanting to pass multiple variables but not wanting to specify them all in the arguments to a function. As far as object-oriented stuff goes, you can specify an array ...