AJAX Contact Form with PHP Fallback
First off, the large portion of the code for this little script was written by other folks, so let me give a shout out to them. I used the FormProcessor.class.php file from Simon Willison's excellent "Easier form validation with PHP " script which I modified slightly (modified file available here: http://www.minc.info/ajaxcontact/FormProcessor.class.phps ). The javascript pre-validation is done using a modified version of Stephen Poley's Javascript form validation script: http://www.xs4all.nl/~sbpoley/webmatters/formval.html. Modified file is available here: http://www.minc.info/ajaxcontact/formval.js. And for the AJAX contact form, I borrowed heavily from Andrew Walsh. I found his script on HotScripts I believe, but I can't seem to find it anymore. Anyway, I think this is his site . Unfortunately, he didn't put his url in the script so I'm not completely certain.
You can see my modified version of the form processor here: http://www.minc.info/ajaxcontact/form_processor.phps .
So, now that the kudos are over…I combined all three methods into one overall contact form. It uses the AJAX version if people have javascript enabled, and defaults back to the PHP form if they don't. And it pre-validates the data to warn users about input that will get rejected by the contact form before they submit.
Unfortunately, there's no good way to test if people have javascript enabled without doing redirects with some javascript code, which means I have to maintain two different pages. That's annoying. I decided an easier method would be to set a cookie via javascript and then look to see if the cookie exists.
The bad part about using the cookie method is it doesn't work the first time people hit that particular page. But, generally, people don't come directly to your contact page, normally they'll go through some other content page before deciding to contact you. Thus, you just have to make sure you put the cookie code into the header of all your pages so that when they decide to contact you, the cookie is there. Unless of course they have javascript disabled. In that case, we want to serve them the PHP version. Now, I came up with a fallback method for the instance of someone coming directly to the contact page as well. If by some chance a user comes directly to your contact page and they have javascript enabled and no cookie, we feed them the PHP form, but with the javascript pre-validation included.
Ok, so that's all fine and dandy, but what happens if the user has javascript disabled and fills out the PHP form incorrectly? Well, we do validation on that as well and kick the user back to the form and warn them that their data is incorrect (and make it pretty plain which data is bad).
By now, you probably want to see what it does. Just remember, the first time you load the page, it's going to give you the PHP form as the javascript cookie will just be getting set. Try out the PHP form (and input some numbers or something in text fields and letters in number fields). Submit it with errors and watch the PHP error handling. Then reload the page and try out the AJAX form. See it in action
Want to see the form page code? Here it is. Download the full code.
Let me know what you think in the comments.
July 6th, 2007 at 5:41 pm
Hi
I am Lucy, I have found your website while searching for some info at Google. Your site has helped me in a big way.
G’night
October 31st, 2007 at 4:00 am
I’ve just noticed that this post is very similar to your previous post: “PHP Contact Form with Javascript and PHP Form Validation”. Is this a later version of the same form?
March 26th, 2008 at 6:30 pm
“Unfortunately, there’s no good way to test if people have javascript enabled.”
Would work for this purpose?
March 26th, 2008 at 6:39 pm
Sorry, it filtered out the NOSCRIPT tag I had in there. Browsers process code within any tags they do not know the meaning of. I believe javascript-enabled browsers know to completely ignore the NOSCRIPT tag. It’s the same reason why some people put javascript code within comment tags in HTML–otherwise, non-js enabled browsers would just output your raw js code.
May 23rd, 2008 at 1:06 am
Hi all. My sincere apologies for not replying sooner. I wasn’t receiving notices about comments for some reason.
@ fotinorod, I’m glad I was able to help out.
@ Ian, yes, this is a new version of the other code I wrote. The original code was a PHP form that did javascript validation prior to posting and php form validation after posting. This code above has that, plus, if the user has javascript, it has a separate form that allows them to submit the form via AJAX rather than submitting it via PHP and having to reload the page. Now, that’s a bit of a misnomer as I’m using PHP with the AJAX form for submission, but the user only sees the AJAX portion of it.
@ Tim, yes, you can use noscript tags, BUT, if you put noscript tags in your header, your code won’t validate. I actually looked at doing it with noscript, but it just wouldn’t work for what I needed it to do and still validate.