Before submit hide section then resume

Viewed 80

I have a form with a few buttons ... One of them needs to hide a section of the form before it's submitted so the requires are not blocking the submission. My problem is that after I hide the section, I can't seem to resume the form submission with the button click value ...

<form id="myform" method="post" action="gstDemandesInscriptions.php?p=2" enctype="multipart/form-data">
    <div class="entete">
        <a class="button" href="gstDemandesInscriptions.php?p=2">Retour</a>
        <button name="mod_cancel">Annuler la demande</button>
        <button name="mod">Enregistrer</button>
        <button name="mod_fact">Facturer</button>
        <button name="mod_term" onclick="beforeSubmit().submit();">Terminer (Ne pas facturer)</button>
    </div>

    <div id="facturation">
        Hide this part before submit
    </div>
</form>

I tryed a few things to get this working ... this is the last thing I tryed

onclick="beforeSubmit().submit();

The javascript for that is

beforeSubmit = function(){
    $("#facturation").hide();
}

What happens right now is that the div hides but the form does not resume submission ...

I tried adding

$("#myform").submit();  

Inside the javascript but then the form is submited but with out the mod_term button value

I also tried

$(this).submit();

But that does not trigger anything

1 Answers

beforeSubmit = function(){ 
  $("#facturation").hide();
  $('<input />').attr('type', 'hidden').attr('name', 'mod_term').attr('value', '1').appendTo('#myform');
  $('#myform').submit(); 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form id="myform" method="post" action="gstDemandesInscriptions.php?p=2" enctype="multipart/form-data">
    <div class="entete">
        <a class="button" href="gstDemandesInscriptions.php?p=2">Retour</a>
        <button type="button" name="mod_cancel">Annuler la demande</button>
        <button type="button" name="mod">Enregistrer</button>
        <button type="button" name="mod_fact">Facturer</button>
        <button type="button" name="mod_term" onclick="beforeSubmit().submit();">Terminer (Ne pas facturer)</button>
    </div>

    <div id="facturation">
        Hide this part before submit
    </div>
</form>

Related