I have a form that submits through ajax. Works fine. However, I am trying to work out how to ONLY submit it if parsley successfully verifies the form.
The issue is that the form submits even if parsley displays an issue. How can I allow for this?
<script>
$(document).ready(function(){
$('#newsletterSubscribe').on('submit',function(e) {
$.ajax({
url:'scripts/newsletter.php',
data:$(this).serialize(),
type:'POST',
success:function(data){
console.log(data);
$("#success").show().fadeOut(20000); //=== Show Success Message==
},
error:function(data){
$("#error").show().fadeOut(20000); //===Show Error Message====
}
});
e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
});
});
</script>
You need to add the Parsley validation condition, as following:
<script>
$(document).ready(function(){
$('#newsletterSubscribe').on('submit',function(e) {
e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
if ( $(this).parsley().isValid() ) {
$.ajax({
url:'scripts/newsletter.php',
data:$(this).serialize(),
type:'POST',
success:function(data){
console.log(data);
$("#success").show().fadeOut(20000); //=== Show Success Message==
},
error:function(data){
$("#error").show().fadeOut(20000); //===Show Error Message====
}
});
}
});
});
</script>
Note also that the Prevent Default behavior is set as a first step in the process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With