Copy select value to input with PHP

0

How to make a select created with PHP send its value to a input created with PHP?

Of this select :

<th>Proveedor</th>
<td>
    <select name="prov_id" id="prov_id">
        <?php
        if (count($lstProvedores) > 0)
        {
            foreach ($lstProvedores as $idx => $campo)
            {
                echo "<option value='{$campo->prov_id}'>$campo->prov_pf_nombre</option>";
            }
        }
        ?>
    </select>
</td>

At this input :

  <?php
  if (count($lstProvedores) > 0) {
      foreach ($lstProvedores as $idx => $campo) {
              echo "<input  value='{$campo->prov_contact_correo}'/>";
      }
  }
  ?>
    
asked by Contreras Angel 24.06.2017 в 00:04
source

2 answers

1

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
	$(function(){
  	$(document).on('change','#mySelect',function(){ //detectamos el evento change
    	var value = $(this).val();//sacamos el valor del select
      $('#myInput').val(value);//le agregamos el valor al input (notese que el input debe tener un ID para que le caiga el valor)
    });
  });

</script>
<select id="mySelect">
  <option value="UNO">Valor uno</option>
  <option value="DOS">Valor dos</option>
  <option value="TRES">Valor tres</option>
</select>

<input id="myInput">

The input must have an ID so you can put the data you are taking out of the select.

    
answered by 24.06.2017 в 01:54
0

First of all PHP runs on the server side, and what you want to do is that when you select a client-side option, an input is automatically filled according to the selection.

for this case it requires javascript or Jquery.

$(document).ready(function(){
    $('.client-select').on('change',function(){
         if($(this).val() != "default"){
            $('.clientuserid').val($(this).val());
        }else{
            $('.clientuserid').val('');
        }
    });
});

where clientuserid is the ID of your html input

If what you want is that when choosing the option, make a query to the server and the answer write it in the input, you should use ajax.

    
answered by 24.06.2017 в 00:23