I am facing a problem that when I submit the page (form) the submission to php mail is success but the page again reloads. Tried return true and false also preventDefault() but still the page reloads.
$('#frmRequest').submit(function(e){
//$('#SUBMIT').click(function(){
var isValidForm = true;
$('#RequestDetailsArea').find('input:visible, textarea:visible, select:visible').each(function(){
if( $(this).val().length == 0|| $(this).val()=='Select' ) {
$(this).addClass('validation');
isValidForm = false;
}
else {
$(this).removeClass('validation');
$('#ErrorRequest').addClass('hidden');
}
});/* end check validation in RequestDetailsArea*/
if (!isValidForm) {
$('#ErrorRequest').removeClass('hidden');
}
validreturn=validateForm();
if(isValidForm && validreturn)
{
$('#RequestDetailsArea').find('div:hidden').each(function(){
//alert($(this).attr('id'));
$(this).remove();
});
msg="ok";
$.ajax({
url:"allfields.php",
type:"POST",
// dataType:"json",
data: $("#frmRequest").serialize(),
success: function(msg){
//alert("Form Submitted: "+ msg);
return msg;
},
error: function() {
alert('Error occured');
}
});
e.preventDefault();
//return false;
}
});/* end of submit Function*/
In your code you have put
e.preventDefault();insideSo I think if your condition doesn’t match then the form will be submitted because the calling of method
e.preventDefault();won’t be executed. If this is the case then you can keep it out of yourifcondition, i.e,Update: You are using
return msgin yoursuccess, that will do nothing, you can do it in this wayand declare the
returnValuesomewhere in the script like