Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to prevent form submission in case of valiadtion error with standard form submission?
    primarykey
    data
    text
    <p>I am developing a form that which includes some textfields and some fields for file uploads. Some javascript code builds the form dynammically by adding it to to a div element (id=wrapper). To my knowlegde one cannot send/upload files via ajax, so I choose the 'classic way' to post the form, see code below. However, I want to have the textfields validated by means of jquery validate; the validation code works fine, but how to prevent the form submission in case of a validation error?. I assume I need somehow to override the standard form submission handler, but dont know how to do that...</p> <pre><code>//original form submit code $("#formNewAgreements").submit(function(){ var form = $("#formNewAgreements"); form.validate(); if(form.valid()){ //only submit via ajax if javascript validation has been performed successfully $.ajax({ type: "POST", url: "suppladmin_agreementsetup_submit_x1.php", data: $("#formNewAgreements").serialize(), dataType: "json", success: function(msg){ if(msg.statusgeneral == 'success'){ $("#wrapper").children().remove(); //remove current New Agreements form SetupAgreements(); } else { $("#errorbox").html(msg.statusgeneral); }//else }, //succes: function error: function(){ $("#errorbox").html("There was an error submitting the form. Please try again."); } });//.ajax //make sure the form doesn't post return false; } //if(form.valid() });//$("#myForm").submit() //validation code for validing the textfields var completeform = $("#formNewAgreements"); completeform.validate(); //html code form &lt;form id="formNewAgreements" name="formNewAgreements" action="submit_x1.php" method="post" enctype="multipart/form-data" target="hidden-form" &gt; &lt;!--&lt;form id="formNewAgreements" name="formNewAgreements" action="" method="post" autocomplete="on"&gt;--&gt; &lt;a href="#" id="add-form"&gt;Add agreement&lt;/a&gt; &lt;div id="wrapper"&gt;&lt;/div&gt; &lt;!--anchor point for adding set of form fields --&gt; &lt;input type="hidden" name="form_token" value="&lt;?php echo $form_token; ?&gt;" /&gt; &lt;input type="submit" name="submitForm" value="Confirm"&gt; &lt;/form&gt; &lt;iframe style="display:inline" name="hidden-form" width="200" height="20"&gt;&lt;/iframe&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload