javascript - Ajax submit after parsley.js validation -
i have form submits through ajax. works fine. however, trying work out how submit if parsley verifies form.
the issue form submits if parsley displays issue. how can allow 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(); //=== avoid page refresh , fire event "click"=== }); }); </script>
you need add parsley validation condition, following:
<script> $(document).ready(function(){ $('#newslettersubscribe').on('submit',function(e) { e.preventdefault(); //=== avoid page refresh , fire 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 prevent default behavior set first step in process.
Comments
Post a Comment