Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda