-2

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jianxee
  • 15
  • 4
  • What's the expected result? – code Mar 14 '22 at 18:36
  • if i click save, it throws validation before it can be send to the server, but if its save draft, just save it without asking a question, so means no validation – Jianxee Mar 14 '22 at 18:38
  • What do you mean by "save"? Store it in local storage? – code Mar 14 '22 at 18:48
  • save means store in database, is save means localstorage? – Jianxee Mar 14 '22 at 18:56
  • So you're sending a request to the server to save your data? – code Mar 14 '22 at 18:57
  • i think question is clearing doing it, do you see anything from other end – Jianxee Mar 14 '22 at 19:16
  • 1
    From the given code, you aren't doing any validation -- if it's happening it must be on the server side. Theoretically you could pass an extra value to the server when you want to skip the validation, and change the server side endpoint to skip validation and just save whatever it gets.... but that's almost certainly a terrible idea (because the unvalidated input could be malicious, or just not fit within your db structure.) – Daniel Beck Mar 14 '22 at 20:58

1 Answers1

-1

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)

mlhDev
  • 2,235
  • 1
  • 22
  • 43