Writing php validation functions that can be used by jquery

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spaggie
Forum Newbie
Posts: 2
Joined: Wed Feb 23, 2011 3:26 pm

Writing php validation functions that can be used by jquery

Post by spaggie »

Basically I'm trying to teach myself php and I have been looking into membership systems (so login form, registration form, etc.) and how to validate these forms. As I understand javascript can be used to give live feedback to the user about the data they are entering into the form. However, my research has shown that most of the time this validation is done independantly from the server-side validation and so there are two validation scripts (one javascript and one php). I only want one validation script and this is obviously going to be php. However, I would like to know how to write the functions so that they can be combined with javascript to give live feedback. I understand this can be achieved through ajax, to call the php function and alter the html in real-time. I have found a tutorial describing how to do just that:

http://jqueryfordesigners.com/using-aja ... ate-forms/

This makes use of the jquery library for the ajax calls which I thought was quite nice. However, I am completely stumped on the php front as this is not included in the tutorial. Completely ignoring javascript, how are validation functions similar to this (i.e. using arrays to output error messages) implimented in the php form? So far I have been dealing with functions that output either true or false based on a tutorial for a complete user login system:

http://www.ineedtutorials.com/code/php/ ... p-tutorial

Unfortunately this code does not output error messages. Essentially I am trying to combine the two tutorials. I want a system that has one validation script that can function without javascript but can be used by javascript to add to the UI experience if it is enabled.

If anyone could help or point me to a better tutorial I would be so greatful. Many thanks.
kristen
Forum Newbie
Posts: 14
Joined: Tue Sep 07, 2010 5:51 pm

Re: Writing php validation functions that can be used by jqu

Post by kristen »

Try looking at JSON more closely. The syntax is a lot more forgiving for a beginner.

In a nutshell you need two sets of validation scripts. The JQuery ones and the PHP ones, plus a method (eg, JSON) to pass the data back and forth between the scripts.

The JQuery validation is strictly for user comfort. The PHP validation is for your comfort. Never trust client side anything. It's easy to spoof a form.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Writing php validation functions that can be used by jqu

Post by Jade »

To display the error messages you'll have to add an echo of the message you want after PHP does the validation. Then in the file with your login/registration form you'll need to add an empty div layer with a ID of something like displayErrorMsg. Then in your ajax once the request has completed successfully you set your empty div layer to have that error message like this:

Code: Select all

 document.getElementById("displayErrorMsg").innerHTML = xmlhttp.responseText;
Post Reply