Pass the value of the combobox to another field

Asked

Viewed 304 times

1

In the code below I pass the value of Nome of the play to $value(detail I’m using the API data because tbm is used in c#) and he is shown in combobox for the user to select which piece he wants:

     <label for="sel1">Peça</label>
      <select class="form-control" id="peca" name="peca">
        <?php
                $json_file =  
                file_get_contents("http://tcc2016.esy.es/API/nomePecas");
                $json_str = json_decode($json_file);
                $lista = array();

                foreach($json_str as $key=>$value)
                {
                    $ID_Peca = ($json_str[$key]->ID_Peca);  
                    $nome_Peca = ($json_str[$key]->nome_Peca);
                    $preco_Peca = ($json_str[$key]->preco_Peca);            
                    $m = array("ID_Peca"=> "$ID_Peca" ,"Nome Peca" => 
                    "$nome_Peca", "Preco Peca" => "$preco_Peca");

                    foreach (array($m['Nome Peca']) as $value) {

                    echo"<option>".$value."</option>";                     
                       echo "<br>";
                    }

                }


        ?>
      </select>
     </form>

But here comes the second part, I need to use the value that the user selected in combobox to take the value of the price of the piece and show to the user in the field below because the piece is already registered with the price, so show the price for it when selecting an item in the combobox, I believe I need to use the ID, but I can’t call him and then call the play:

  <div class="form-group">
      <label for="inputdefault"><font color="#8e1c1c">Preço</font></label>
      <label class="form-control" id="inputdefault" type="number" name="preco">
          <?php


            ?>
        </label>
  </div>

I saw things like using java script, like:

<script>
    function f()
     {
       var element = document.getElementById("peca").value;
       var seletedValue = element.options[element.selectedIndex].text;
       window.location = "cadastro.php?parametro=selectedValue;"
     }
   </script>

The problem is I have no idea how to use it or call turn it to the php.

I’ve been trying to make this work for many hours and this is all I need to finish, but I’m not getting it!

  • Read the question you wrote and see that without knowing the context... how can we help? Show what you are looking for and how, so we can answer.

  • I improved the question so if I can review it would be very helpful

1 answer

0

Hello! tries to do so in your select: < select onchange="f()"> because when selecting the option will trigger its function f()

Browser other questions tagged

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