PHP print Value of Select according to variable

0

I already show the values of my database in my table correctly but I would like that if I select the option with the id = 1 show the information with an echo related to that information, something like this is my code:

    <?PHP
//esto es un ejemplo , la información funciona 
//correctamente y trate de no saturar con código de mas
     $id = el numero id de usuario;
     $telefono = el teléfono del usuario
    ?>

Here is my select

<select>
//** aqui ya tengo que el $id corresponda al usuario **
  <option value="">select</option>
  <option value="<?PHP echo $id; ?>">Pablo</option>     //id =1
  <option value="<?PHP echo $id; ?>">Diego</option>     //id =2
</select>

Followed by my select I would like to display a message based on the option you choose

<?php
if ($id == '1') { echo 'el telefono de Pablo es :' . $telefono; }
if ($id == '2') { echo 'el telefono de Diego es :' . $telefono; }
else{ echo 'aqui no se muestra nada'; }  ?>
    
asked by claus vargas 10.09.2018 в 19:41
source

2 answers

1

That is no longer part of PHP. The manipulation of the DOM can be done with Javascript or with the Jquery library, but if you want to show according to your query result and be able to choose the option in the SELECT , you can do the following:

Bring the data of who is the selected option and through the validation in each of the options print "Selected".

 <select>
  <option value="">----Elige----</option>
  <option value="1" <?php if($myselect==1){echo "Selected";}?>>juan</option>
  <option value="2" <?php if($myselect==2){echo "Selected";}?>>pablo</option>
  <option value="3" <?php if($myselect==3){echo "Selected";}?>>pedro</option>
  <option value="4" <?php if($myselect==4){echo "Selected";}?>>maria</option>
  </select>

Now, as I told you jquery / javascript is ideal to manipulate the DOM , whether you want it or not, you will have to implement it by obligation and more. trying php look how simple it is to use javascript / Jquery

$(document).ready(function(){

   $("#myselect").change(function(){//obtener valor del select en el evento on change
   
      alert($(this).val())//mostrar el valor en un alert de el select
   
   })

})
<select id="myselect">
  <option disabled >select nombre</option>
  <option value="pedro">select</option>
  <option value="pablo">Pablo</option>
  <option value="diego">Diego</option>  
</select>



 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

I hope you can use my example Greetings!

    
answered by 10.09.2018 / 20:29
source
0

You can use:

<select name="directorio">
//** aqui ya tengo que el $id corresponda al usuario **
  <option value="">select</option>
  <option value="<?PHP echo $id.'|'.$telefono; ?>">Pablo</option>     //id =1
  <option value="<?PHP echo $id.'|'.$telefono; ?>">Diego</option>     //id =2
</select>

And on the php side you should use:

$arr_data = explode('|',$_POST['directorio']);
$id = $arr_data[0];
$telefono = $arr_data[1];
if ($id == '1') { echo 'el telefono de Pablo es :' . $telefono; }
if ($id == '2') { echo 'el telefono de Diego es :' . $telefono; }
    
answered by 11.09.2018 в 17:49