I have added the client side validations on a form using jQuery's validate() function and assigned rules and it shows the error messages in Red color below the respected fields. I want to show same error messages after performing server side validations done in PHP. Please help.
4 Answers
You'll have to perform that logic with PHP, in a sense replicating all that jQuery's validate() plugin does. I generally run through a series of tests, and add errors to an array:
$errors = array();
if (!valid_name($firstname)) $errors[] = "Please provide your first name.";
if (!valid_email($email)) $errors[] = "Please provide a valid email address.";
Then, after all of my rules, I'll decide whether or not to post the data or show the errors:
if (count($errors) > 0) {
show_errors($errors);
show_form();
} else {
submit_data();
}
This is a format that has worked well for me.
1 Comment
There's no automatic way (unless someone built one) that can replicate the client-side validation to server-side.
You might want to see how someone manage to come out with a working solution here:
jQuery for Real time Server-side Form Validation
Comments
Is your goal to validate an individual field via a call to your server before you submit the entire form? Essentially you will need to perform a post ($.post) for each piece of data that you want to validate. This link is to the jQuery documentation for Ajax posts.