Form Validation With Jquery And Ajax
I am using AJAX with JQUERY to call a PHP script to validate a user email. But, for some reason, the form submits even when it shouldn't. What am I doing wrong? I know the error is
Solution 1:
Try updating these:
$("#signup").submit(function(e) { //<----pass the event here as "e"
e.preventDefault(); //<----stops the form submissionvar error= false;
var dataString = $(this).serialize();
var email= $.trim($("#email").val()); //<----use trim this way
Solution 2:
If you absolutely have to use AJAX for form submission, this might be a better way to do it:
$('form').submit({
$.ajax({
type:'post',
url: 'someurl.php',
data: dataString,
context: this, // this here refers to the form object
success:function(data)
{
// perform your operations hereif(something_is_wrong)
{
// show message to user
}
else
{
this.submit(); // put this code in the block where all is ok
}
}
});
returnfalse; // makes sure the form doesn't submit
});
Post a Comment for "Form Validation With Jquery And Ajax"