PHP Class and Ajax in action!!

sulman8s

I am a php class that is validating input fields.. I want to use ajax ..

Details:

ValidationClass.php
Regis.php // registration page

as my form action is self... so i am validating on the same page!!!

Should i create a separate page for AJAX response! as ajax check the echo's from any specific page!!

More if i want to get the response for two fields should i write the ajax two times?

Any hint or a standard way? Plz

NogDog

I would submit all the fields as once (e.g., submit the whole form) to the validation PHP script, and have the script return a JSON object with all the validation results, which the JavaScript response handler could then loop through.

sulman8s

but if i want to append the error messages at the front of each field how can i do.. more check the availability of any specific field like email?

NogDog

At that point, it really becomes a JavaScript question. If you're using JQuery, it should be relatively easy to loop through the JSON object and apply it to the form elements -- just make sure that (a) you have useful id elements in each HTML form element so that your JQuery can easily reference them, and that your PHP returns those ID's in its response elements (e.g. it's easiest to make the ID and the form name the same:

With that, if you get an error message for "last_name", you could just reference "last_name_error" as the element to add your error text to (or clear its value if that field no longer has an error but some other field does).

And I suspect the real JavaScript experts here (I'm a hack, at best) could give you better ideas.

sulman8s

what do u mean by this

//If you're using JQuery, it should be relatively easy to loop through the JSON object

how to use to generate errors at the front of each field on keyup ?

ShrineDesigns

html5 has built-in validation for form fields and there are polyfills for non-compliant browsers.
that should be enough to "generate errors at the front of each field on keyup".
in a situation where a user signs up or logs in, i would rely on php to validate authentication.
10 times out of 10 you can bypass javascript, not to mention you can submit the form to the server without a web browser or even from the page the form is on.

sulman8s

on sign up i just want to check the availability of my two fields

1-email:
1-DepID:

check from database and then show messages at the front of input fields?

i am using ajax for this.. i dot want to write ajax call two times for validation from database..