Feb 12

One of the more recent coding projects I’ve been working on is the redesign of the our customer signup process at work. During this process, a lot of data validation & integrity needs to take place from the user entered information coming from the HTML forms. For a PHP/Javascript coder, this scenario has always meant, “Do I validate in PHP? Javascript? Both?” PHP gives you database access but it comes at the price of having to submit the page. Plus, you have to build in extra navigation logic which can sometimes be messy. Javascript is quick & easy, but can be unreliable (user can turn javascript off) and you do not get any access to a database. If you do both, then you split up the validation between specific fields and that becomes flat out messy.

So which route did I go to accomplish this validation task? None. Well, actually all of them, but not by the traditional means. Rather, the best method I found here was to use an AJAX method and split the display (HTML), trasportation (Javascript), and validation (PHP) layers up and keep them completely seperate. By using this method you have a lot of advantages;

  • Because AJAX is as asynchronous, user does not have to submit the form (and wait for a reload). The less clicks for the user, the better.
  • It makes the experience is as smooth as possible for the user
  • Seperation of view (HTML), transportation (javascript), and validation (PHP) layers to make coding much cleaner.

Once I had an understanding of what I needed to build, here is the basic outline of the steps needed to design this app…

  • Be able to convert the HTML form into XML
  • Use the XMLHTTPRequest object to POST an XML string to a PHP script
  • Within PHP, validate data using regular expressions and database queries
  • Denote valid & invalid data values only by adding “valid” and “error_message” attributes to the element nodes and not altering any of the original XML data.
  • Send the same (but slightly modified) XML document back to the Javascript
  • Within Javascript, determine what (if any) errors the user may have with the data entered
  • Inform user of errors

I also had the goal of making this validation process as portable/reuseable as possible and be able to add it to any form by only adding a “onsubmit()” attribute to the HTML form and the error message <div> to the HTML document. Aside from that, everything else takes care of itself in terms of XML creation, validation, error message(s) being displayed, and the input boxes being highlighted within the form.

So here’s a link of a stripped down version of what was built. It isn’t pretty and isn’t meant to show off my design skills. Rather is it designed to demo the functionality and behavior of the script, and then provide the source code (with comments) to build a much more useful app on top of the existing code. Play around with it, modify it, break it, & fix it. That is usually the best way to learn.

Post in the comments below or email me if you have any questions

(Update: I just checked the example page for the first time in a long time, and something is broken. I’ll update again when it is fixed.)

written by Derek \\ tags: , , , , ,

Oct 29

Brad Neuberg has published an article on O’Rielly’s OnJava.com. In this, he presents a solution that has long hampered AJAX web development. When using GMail, ever accidently click the back/forward button out of habit only to leave the gmail site? It is quite an annoying reality that we’ve had to live with because nobody had been able to fix how to navigate around abstract applications like Gmail. Well now, at least one solution has been presented. In quickly looking through the code, everything appears to make sense, but it seems like it might be a bit tricky to implement. Check out two of the demos here and here.

This is interesting because just last night I was listening to one of the recent Web 2.0 podcasts and one of the hosts or the interviewee (a Flock developer) said the AJAX/history button problem lied a bit further upstream and was something the browser developers would have to fix. While that would be optimal as it would not require use to code the hack, I wouldn’t even know how they could begin fixing that issue.

Let the true web applications begin!

Gotta run. Time to head to Lawrence to watch my Jayhawks kick a little Tiger tail in a few hours.

written by Derek \\ tags: , , , , ,