PHP - Llenar un input a partir de un select

 
Vista:

Llenar un input a partir de un select

Publicado por Kelvin Hernandez (2 intervenciones) el 05/11/2018 14:22:15
Hola! Estoy trabajando en un formulario modal de bootstrap, y tengo la selección de servicios los cuales los muestra en las opciones del select extrayendolos de una tabla, hasta ahi vamos bien, lo que quiero es que en el input llamado costo imprima el costo del servicio que esta en la consulta en mysql para poder insertarlos en otra tabla desde el formulario. Aqui les dejo el código...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<div class="form-group">
<label for="plan">Plan:</label>
<?php
$conexion = mysqli_connect("localhost", "root", "");
$db = mysqli_select_db($conexion,"flp_fns");
$planes = mysqli_query($conexion, "SELECT id_servicio,nombre_servicio,costo from servicios");
echo ("<select class='form-control' id = 'plan' name = 'plan' type = 'text'>");
while ($plan = mysqli_fetch_array($planes))
{
echo "<option value = ".$plan['nombre_servicio']."> ".$plan['nombre_servicio']."  Cuesta $".$plan['costo']." </option>";
echo ("<input id='costo' name='costo' type='text' value =".$plan['costo']." >".$plan['costo']."</input> ");
echo("</select>");
?>
</div>


Obviamente el input llamado costo no funciona, quiero mostrar el costo en ese input, si alguien me pudiera resolver ese ejemplo me seria muy util, gracias de antemano.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Demon Jr
Val: 31
Ha aumentado su posición en 9 puestos en PHP (en relación al último mes)
Gráfica de PHP

Llenar un input a partir de un select

Publicado por Demon Jr (11 intervenciones) el 05/11/2018 16:32:23
Hola brother espero y te sirva este ejemplo, igual te adjunto el codigo completo con la base de datos de ejemplo


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html lang="es" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>  Inputs Dinamicos </title>
  </head>
  <body>
<form class="" action="inputs_dinamicos.html" method="post">
 
<center><h2> Inputs Dinamicos</h2> </center>
 
<!-- SE CREA EL ENCABEZADO DE LA TABLA -->
 
<center>
<table border =1>
  <tr>
    <td> ID</td>
    <td> NOMBRE</td>
    <td> APELLIDO</td>
</tr>
<tr>
 
 
<?php
//SE HACE LA CONEXION A LA BASE DE DATOS
$server = "localhost";
$user ="root";
$psw="";
$db="dbhelps";
$conexion=@new mysqli ($server,$user,$psw,$db);
 
// SE VALIDA QUE LA CONEXION SE EFECTUE
if($conexion-> connect_error){
  die('Ups ! Sorry I cant Connect to the Database , Trouble -> ' . $conexion->connect_error);
}
 
// SE CREA UNA VARIABLE QUE TENDRA LA CONSULTA A LA TABLA
$SqlGetData="select tblnId as ID, tblnNAme as NOMBRE , tblLastName AS APELLIDO from tblnombres";
//SE EJECUTA LA CONSULTA
$ResultQuery=$conexion->query($SqlGetData);
 
// SE REVIA SI LA COLSUTA DEVUELVE AL MENOS UN REGISTRO
if ($ResultQuery->num_rows > 0) {
  // SI TRAE REGISTROS HACE UN ARREGLO Y LO VA RECORRIENDO CON EL WHILE
  while ($row = $ResultQuery->fetch_array(MYSQLI_ASSOC)) {
    // SE CREAN VARIABLES PARA IR ALMACENANDO LA INFORMACION QUE CONTENGA CADA REGISTRO
        $id=$row['ID'];
        $nombre=$row['NOMBRE'];
        $apellido=$row['APELLIDO'];
 
// SE CREA EL RESTO DEL CUERPO DE LA TABLA Y SE LE CONCATENAN LOS VALORES
 echo "
<td><Input type='text' name= $id value=$id></td>
<td><Input type='text' name= $nombre value=$nombre></td>
<td><Input type='text' name= $apellido value=$apellido></td>
</tr>
 ";
 
/* NOTA:
         POR CADA VUELTA QUE DE EL CICLO IRA AGREGANDO UNA FILA

*/
  }
     // UNA VEZ QUE TERMINA EL CICLO SE CIERRA LA TABLA
  echo "
  </table>
  </center>
  ";
 
 
}
 
   else {
     // SI NO EMCUENTRA REGISTROS SOLAMENTE IMPRIME UN MENSAJE
     echo "<center> <h2> No Se Encontraron Registros </h2> </center>";
   }
 
 
 
 
$conexion->close();
 ?>
 
</form>
  </body>
</html>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar