I am trying to validate a form using jquery by calling preventDefault(). After addressing the validation error, what I want is to submit the form when the submit button is pressed again, but for some reason it no longer submits. Here is my code:
html:
jquery:
$(document).on('click', '#go', function(){
var inputA= $('#inputA').val();
var inputB= $('#inputB').val();
if(inputA != inputB){
alert("not same!");
$(document).on('submit', '#myForm', function(e){
e.preventDefault();
});
}else{
$(document).on('submit', '#myForm', function(e){
if( $(this).hasClass('form-submitted') ){
e.preventDefault();
return;
}
$(this).addClass('form-submitted');
});
$("[name='message']").html("Please wait, i am being submitted.");
}
});
php on submit:
if(isset($_POST['go'])){
//do stuff here
echo '';
echo '';
}
As you can see, i use preventDefault() when the values are not correct. I can change it then press the submit button again, but it only shows the message "Please wait, i am being submitted." and does not reload to go to the success script. Is there something wrong with my code? I really would like to not resort to ajax on this one.
P.S. the addClass 'form-submitted' is a precaution for not double submitting i found here in stackoverflow.