i have question in ajax , html forms, , need helps please.
i'm using parsley validation (http://parsleyjs.org/) validation rules. probleme validation doesn't support database validations : instant, have check if field 'nom' exist in database or no, if exist (show message) , if don't exist submit form myaction.php.
my ajax script :
<script> $(document).ready(function () { $("form").submit(function (e) { e.preventdefault(); //prevent default form submit var nom = $("#nom").val(); $.ajax({ type: "post", url: 'ajax.php', data: "field="+ nom , success: function (data) { if(data==1){ alert(data); }else if(data==0){ cache: false $('form#form_id').submit(); } }, }); }); }); </script>
my html form :
<form action="myaction.php" method="post" name="form_name" id="form_id"> <input type="text" id="nom" name="nom" required value=""> <input type="text" id="prenom" name="prenom" required value=""> <input type="text" id="remarque" name="remarque" required value=""> </form>
ajax.php : return 0 if field doesn't exist in database , 1 if exist
the probleme face ajax called in loop. in each time form submited , call ajax. want if data equal 0 (field doesn't exist in database) form must submited myaction.php
here 1 solution.
basically you're submitting , handling submision forever. need break when data has been validated. since care nom
, set flag on can check whenever you're submitting
$(document).ready(function () { $("form").submit(function (e) { var nom = $('#nom'); var valid = nom.data('valid'); if (valid === 0) { // valid, let's continue return; } else { // valid undefined never set e.preventdefault(); //prevent default form submit $.ajax({ type: "post", url: 'ajax.php', data: "field="+ nom.val() , success: function (data) { if(data==1) { alert(data); } else if(data==0) { nom.data('valid', 0); } } }); } }); });
Comments
Post a Comment