Pass the value of the combobox to another field

1

In the code below I pass the value of Nome of the part to $ value (detail I am using the API data because it is used tbm in C #) and it is shown in combobox to the user select which part either:

     <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 get the value of the part price and show the user in the field below because the part is already registered with the price, so show the price of it for it when selecting an item in combobox , I believe I need to use ID , but I can not call it and then call the part:

  <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've seen things like using java script, type:

<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 that I have no idea how to use this or call transforming it to php .

I've been trying to make it work for many hours and it's all I need to finish, but I can not do it!

    
asked by anonymous 19.11.2016 / 20:46

1 answer

0

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

    
21.11.2016 / 14:56