2 different structures with select field

Asked

Viewed 205 times

1

I need 2 forms on the same page and I am trying at the end to create a select field so I can choose between one or the other, I am trying here several ways without success ! Please help, please follow:

<div class="module-body" id="div1">
    <section class="docs">
        <form method="post" action="" class="form-horizontal row-fluid">
            <input type="hidden" name="codigoPlanoEscolhido" value="<?=$codigoPlano?>" />
            <input type="hidden" name="nomePlanoEscolhido" value="<?=$nomePlano?>" />
            <input type="hidden" name="tipoPlanoEscolhido" value="<?=$tipoPlano?>" />


   <input type="text" name="clienteEmail" value="<?php  $telefone = '55' . $cliente->telefones[1]->ddd .       $cliente->telefones[2]->numero;

    echo '(22)'.("******"), substr($telefone, -3).''

      ;?>" class="span3" disabled />
        <br>

            <button name="confirmarPedido" class="btn btn-primary btn-lg">Confirmar Minha Solicitação Por SMS</button>
        </form>

<div id="div2" class="module-body" style="display:none">
            <form method="post" action="upgrade-3">
                <input type="hidden" name="codigoPlanoEscolhido" value="<?=$codigoPlano?>" />
                <input type="hidden" name="nomePlanoEscolhido" value="<?=$nomePlano?>" />
                <input type="hidden" name="tipoPlanoEscolhido" value="<?=$tipoPlano?>" />
                <button name="solicitarUpgrade" class="btn btn-primary btn-lg">Confirmar solicitação por Email</button>
            </form>

            <script>
                function oWhichSubmit(thisSubmit) {
                    var oWhich = thisSubmit.value;
                    if (document.getElementById(oWhich)) {
                        document.getElementById(oWhich).click();
                    }
                }
            </script>

            <select onchange="oWhichSubmit(this)" onkeypress="oWhichSubmit(this)">
                <option value="confirmarPedido"></option>
                <option value="confirmarPedido">Submit1</option>
                <option value="solicitarUpgrade">Submit2</option>
            </select>
            <input type="submit" name="confirmarPedido" id="upgrade2" value="confirmarPedido" />

     <?=getClienteEmail($cliente)?>"  class="span3" disabled/><br>
 <button name="solicitarUpgrade"  class="btn btn-primary                        tn-lg">Confirmar solicitação pelo Email</button>
                </form>
          "  class="span3" disabled/>
           <input type="submit" name="solicitarUpgrade"        id="upgrade3" value="solicitarUpgrade" />
            </form>

 <select id="mySelect">
 <option value="div1">div1</option>
<option value="div2">div2</option>
</select>


 <script>

document.getElementById("mySelect").addEventListener("change",       myFunction);
function myFunction() {
   var x = document.getElementById("mySelect").value;
   if(x=="div1"){
    document.getElementById("div1").style.display="block"; 
  document.getElementById("div2").style.display="none"; 
    }else if(x=="div2"){
    document.getElementById("div2").style.display="block"; 
  document.getElementById("div1").style.display="none"; 
    }
}
 </script>

I need you to select at the end me the option to choose between one and the other and do Submit

  • The two forms would be the #upgrade2 and #upgrade-3? If yes, they are exactly the same form, just changing the text on the button, then you can have only one form and include the field select in the same.

  • So, my friend, it’s 2 languages, at the top I do an authentication by SMS ,, at the bottom I do the simple authentication by email, then I want to separate the 2 with select at the end and not knowing how to do, edit there

1 answer

2


Put each form inside different Ivs with None display:

<select id="mySelect">
  <option value="div1">div1</option>
  <option value="div2">div2</option>
</select>
<div id="div1" style="background:red;height:100px;width:100px;">
  <form></form>
</div>
<div id="div2" style="display:none;background:blue;height:100px;width:100px;">
  <form></form>
</div>

Javascript:

document.getElementById("mySelect").addEventListener("change", myFunction);
function myFunction() {
    var x = document.getElementById("mySelect").value;
    if(x=="div1"){
        document.getElementById("div1").style.display="block"; 
      document.getElementById("div2").style.display="none"; 
    }else if(x=="div2"){
        document.getElementById("div2").style.display="block"; 
      document.getElementById("div1").style.display="none"; 
    }
}

I made that example: https://jsfiddle.net/k8ebn4ja/

  • is worked out, but in form2 <div id="div2" style="display:None"><form method="post" action="upgrade-3"> it does not appear !

  • Let me see how your code is now, because it gives way q is up there the div upgrade3 is inside the upgrade2, then if it is this way will not even appear...

  • edited la see how this

  • You’re not shutting down div1, div2, or Section. Try this way: closes the Section after the div1 after the </form> and the div2 after the second </form>

  • Thank you very much Jessika, you saved me here

  • Just to complement, I would add an id to each form and after selecting the form would make the Submit inside the if: document.getElementById("form1").submit();

  • Ow adds a button like Submit inside each form, then each Submit button will perform the action of the form it belongs to.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.