Am trying to check a form submitted using jquery and php. Am making an .ajax request to the server to validate if data have been entered to the form and perform different operations depending on the callback response of the server. e.g show error message if firstname is not entered or lastname is not entered.
The problem is that when both are not entered the callback return's the message firstname and lastname together without revealing the error messages.
// JavaScript Document
$(function(){
$("#form").submit(function(event){
$("#name_alert").css("display,none");
event.preventDefault();
$.ajax({
url:'functions/register_user.php',
type:"POST",
data:$(this).serialize(),
success:function(data){
if(data=='true')
{
$("#form").effect("shake", {times:2}, 600);
$("#alert").fadeIn("slow");
$("#note").fadeIn("slow").html('<strong>ERROR</strong>: Your details were incorrect.');
}
if(data=='name'){
$("#name_alert").fadeIn("fast").effect("shake", {times:1}, 600);
$("#name_note").html('<strong>ERROR</strong>: Your first name must be in the range
of 2 to 20 characters long.');
}
if(data=='surname'){
$("#surname_alert").fadeIn("fast").effect("shake", {times:1}, 600);
$("#surname_note").html('<strong>ERROR</strong>: Your surname must be in the range
of 2 to 20 characters long.');
}
else {
$("#name_alert").fadeOut("fast");
$("#note").html('<strong>PERFECT</strong>: You may proceed. Good times.');
}
}
});
});
});
Server side code is checking if fields are empty or not from the form and echoing the corresponding messages like firstname or lastname. Afterwards jquery shows the right message depending on the output of the server.