Juggling Javascript and PHP’d Off

Help…. more code conundrums. The more I try and figure something out the deeper I get (but still no solution). I mentioned here that I was working on the Week 7 PHP challenge exercise for the guestbook, which involved validating the user’s input into the phone field, ie, .checking if the number is all numbers, or numbers and spaces only, the correct number of digits and give a useful error message if the wrong number is entered’ (UTS Blackboard, 2012). So far, I haven’t solved it, despite spending a lot of time on it along with searching heaps of resources.

Although I won’t be asking users of my site for their phone number, it is important to validate user input to avoid having a inaccurate (ie useless) database, which could even contain malicious content (Welling L & Thomson L, 2009, p25). I will be asking for an email address, however, so I do need to solve this problem. The least clunky solution I found, both for email and numeric validation, is here, using  ‘preg_match … or die’ and I thought if I could just put a ‘try again’ link on the error page that took a user back to the guestbook , that may be a reasonable solution. However,  on this page I found a good argument against using ‘or die’.

I also found information regarding form validation, ie, that you should use JavaScript to validate the input in a form before it gets to the server (thus saving server load (w3schools.com, 2012) and preventing errors from getting to the database, as well as PHP validation to prevent the database from filling up with errors, invalid data and/or malicious code, etc  (Welling L & Thomson L, 2009, p25). Phew. Of course. That makes complete sense, and given that a user can turn off JavaScript,  it would be logical to have more than one validation point.

I noticed in the gb.php file we were supplied that there is a JavaScript validation to prevent empty Name and Message fields, so I included all fields in the code. Ok, so that works. No empty fields. This website seemed to have the solution to the phone field input (you do need to view source to get the code) but I couldn’t get it working in my example.

Thinking this could help, and maybe I should just try building a form from scratch…

And for alert boxes that contain all the errors within a form (using PHP), this looks useful.

Now. Stepping. Away. From. The. Code.

So, I have to solve the following:

  • validate the form input on the client side using JavaScript
  • validate the form input on the server side using PHP

Julie

Any offers of help greatly appreciated!

PS On the positive side, I did get a ‘date last modified’ function into my index page (I had a few attempts to change the date format, but no success. Then I found some code to do it, but it was incredibly long and detailed and I couldn’t get it to work. There has to be a shorter more efficient way?)

References

Field Validation with JavaScript viewed 4/24/2012 2012, <http://www.web-feats.com/classes/html2/javascripts/validate.htm>.

How to Validate a Form Using PHP Part 1: Complete with Error Messages! | Build Internet viewed 4/24/2012 2012, <http://buildinternet.com/2008/12/how-to-validate-a-form-complete-with-error-messages-using-php-part-1/>.

PHP Forms viewed 4/24/2012 2012, <http://www.w3schools.com/php/php_forms.asp>.

PHP Freaks – PHP Help or die() must die viewed 4/24/2012 2012, <http://www.phpfreaks.com/blog/or-die-must-die>.

Prasanth 2011, ‘JavaScript Form Validation : quick and easy! ‘, Javascript-Coder.com, viewed 4/24/2012, <http://www.javascript-coder.com/html-form/javascript-form-validation.phtml#custom>.

UTS Blackboard. 2012, Digital Media Technologies, Subject Materials, Week 7, viewed 24 April 2012, via login at <https://online.uts.edu.au>

Validating E-mail and URL viewed 4/24/2012 2012, <http://myphpform.com/validating-url-email.php>.

Welling, L. & Thomson, L. 2008; 2009, PHP and MySQL Web development, 4th edn, Addison-Wesley, Upper Saddle River, NJ.

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: