Take the data from a select and declare variable

Asked

Viewed 427 times

0

all right? I am very new to programming and have a simple question that I did not find here on the forum. I have this code below the view (codeigniter):

<div class="span6">
      <label for="formaPgto">Forma Pgto</label>

      <select name="formaPgto" id="formaPgto" class="span12">
        <option value="Dinheiro">Dinheiro</option>
        <option value="Cartão de Crédito">Cartão de Crédito</option>
        <option value="Cheque">Cheque</option>
        <option value="Boleto">Boleto</option>
        <option value="Depósito">Depósito</option>
        <option value="Débito">Débito</option>        
      </select> 

  </div>

I want that when the person chooses the Check option, open a field below, with an input type text, to enter the check number (OBS: I already created the column numCheque and numCartao in the BD). And the same when the user chooses Credit Card.

I couldn’t fix it. If anyone can help me, I’m very grateful.

  • 1

    You could solve this with javascript. Check out this answer: http://answall.com/questions/93779/mostrar-e-esconder-input-para-campo-de-pesquisa-em-javascript I hope I helped, MDC

  • Thanks for the help friend... But with JS you could record in BD? Or just with php same?

  • You create the input normally like you created the others and the only thing that javascript does is hide it or show it in the onclick of the check option, with php Voce takes normal by name, and just as if it were always visible

  • If you want to save to the BD, use $ajax to save to the bank via PHP. http://api.jquery.com/jquery.ajax/

  • Guys... I’m trying to make the fields start hiding but it’s not giving. So q opens the "modal" of billing, already appears the fields. I already went to CSS and put it as a display: None, and still keeps appearing. I went to JS and created a function to hide the fields with this.style.display="None" and it’s still there. As for the fact of recording in the comic book, I do for control and at first it is working.

1 answer

1

I’ll rework the answer with all the code for this case:

<div class="span6">
      <label for="formaPgto">Forma Pgto</label>

<select name="formaPgto" id="formaPgto" class="span12" onchange="yesnoCheck(this);">
        <option value="Dinheiro">Dinheiro</option>
        <option value="Cartão de Crédito">Cartão de Crédito</option>
        <option value="Cheque">Cheque</option>
        <option value="Boleto">Boleto</option>
        <option value="Depósito">Depósito</option>
        <option value="Débito">Débito</option> 
</select>
<input type="text" name="numCheque" id="numCheque" style="display: none">
  </div>

<script>
function yesnoCheck(that) {
    if (that.value == "Cheque") {
        document.getElementById("numCheque").style.display = "block";
    } else {
        document.getElementById("numCheque").style.display = "none";
    }
}
</script>

I’m sorry if any confusion, MDC.

PS:I tested the code and it works perfectly, just you take it and paste it into a file with extension . html.

Source:https://stackoverflow.com/questions/29321494/show-input-field-only-if-a-specific-option-is-selected

  • Thank you very much MDC. I will test here and anything I put on. God always bless you in everything!

  • It worked perfectly buddy. Thank you very much!

  • You’re welcome to @Jardeldint if you need any more help. Luck for your projects, MDC (or Onion Moca :)

  • Buddy... without bothering you again... I tried to do the same for Credit Card. But putting the same code, just changing from Check to Credit Card (with spaces and accent), the same does not happen. if accepts string with spaces and tals?

  • I found my mistake..... kkkk... had forgotten to input the credit card... kkkk.. Sorry and thanks again!

Browser other questions tagged

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