Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
jQuery to handle two submits one with validation and another without validation

I have a form as:

<form method='post' action="submit.cfm" id='formid' class="formclass" name="formname">
    <input type='submit' name='submit' value="save">
    <input type='submit' name='submitDraft' value="save draft">
</form>

Now without jQuery, I can submit the form and it will validate with html required fields for whichever fields it needs to be, and I will fill it and save it, now for the save draft, I want to save it without validation and trying to use jQuery to accomplish this but it's not working as expected.

I tried like this:

$("#formid").submit(function(event) {
    event.preventDefault();   // prevent default action 
    var post_url = $(this).attr("action");   // get form action url
    var form_data = $(this).serialize();     // Encode form elements for submission
    
    $.post( post_url, form_data, function( response ) {
      $("#server-results").html( response );
    });
});

Now can I kill 2 birds with one stone, any feasible solution? There is no upload field, so I am safe from that perspective.

More tried came up with

<script type="text/javascript">
$(document).on('submit','#formid',function(event) {
   event.preventDefault(); //prevent default action 
   var _id = $(this).find("#saved").attr('id');
   if(_id == 'saved') {
      alert('hi');
      var isDrafted = 0;
      $.validate({
         modules : 'date, file'
      });
   } else {
      alert('hello');
      var isDrafted = 1;
      var post_url = $(this).attr("action"); //get form action url
      var form_data = $(this).serialize() & '&isDrafted=' + isDrafted; //Encode form elements for submission
      
      $.post( post_url, form_data, function( response ) {
        $("#server-results").html(response);
      });
   }
});
</script>

Now I am using a formvalidation.net validator, its old but its working, and honestly it is going to be mess to replace it, so how can I make this work?

Something else noticed, I had to click the button twice before it shows validation messages when clicked on save.

And the whose context is save means submit to the action page to save in database

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You would need to name the buttons the same with different values, like mentioned here:

<input type='submit' name='submit' value="save">
<input type='submit' name='submit' value="save draft">

Or, since you are open to jQuery, setting a hidden field to which submit button was pressed:

<input type='hidden' name='whichSubmit' id='whichSubmit'>
<input type='submit' name='submit' value="save">
<input type='submit' name='submitDraft' value="save draft">
$("#formid").submit(function(event){
    $('#whichSubmit').val(event.target.name);
});

(both of those are untested pseudo code)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!