Show two data of the same table in different INPUT

1

I have a table ARTICLES that has two fields: the first Article (whole number) the second Description (varchar)

In my system I show a select with all the records of the table ARTICLE By selecting I'd like to:

A textbox that I have will show the description of the article, contained in the table ARTICLES

This table is already with records.

This is my code

<?php
include "conexion.php";
global $cone;
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>

    <title> </title>
</head>
<body>
<h1 align ="center">Bienvenido</h1>
<a href ="index.html"><ol>Pagina Principal</ol></a>
<a href ="registrar.php"><ol>Registrar</ol></a>
<a href ="listado.php"><ol>Listado</ol></a>
<a href ="Sancion.php"><ol>Sancion</ol></a>
<form method="POST" action ="procesar4.php">     
    <label> Ingrese cedula del sancionado</label>
    <input type="text" id="cedula" name="cedula1"><br/>

    <label> Ingrese cedula del sancionador</label>
    <input type="text" id="cedula" name="cedula2"><br/>

    <label> Ingrese tipo de sancion </label>
    <div><select name="sancion">
<?php
$registros=mysqli_query($cone,"select * from sanciones");
while ($reg = mysqli_fetch_array($registros)){
echo "<option value='$reg[id_sancion]'>"."$reg[sancion]"."<br/>"."</option>";
}
?>
</select></div>
<label> Ingrese articulo inflingido </label>
<div><select name="articulo" id="articulo">
 <script type="text/javascript">
      jQuery(document).ready(function($) {
        // Change es un evento que se ejecuta cada vez que se cambia el valor de un elemento (input, select, etc).
        $('#articulo').change(function(e) {

          $('#descripcion').val($(this).val());
        });
      });

</script>


<?php
$registros=mysqli_query($cone,"select * from articulos");
while ($reg = mysqli_fetch_array($registros)){
echo "<option value='$reg[id_articulo]'>"."$reg[articulo]"."</option>";

}
$sql =mysqli_query($cone,"SELECT des_articulo 
FROM articulos 
WHERE id_articulo=id_articulo");
$sql2 = mysqli_fetch_array($sql);
echo "<br/>"."<input type='text' id='descripcion' readonly value='$sql2[des_articulo]'>";

?>

</select></div>  


 <fieldset>
        <legend>Ingrese estado de sancion</legend>


<?php

global $cone;
$registros=mysqli_query($cone,"select * from estado_sanciones");
while ($reg = mysqli_fetch_array($registros)){
   echo "<label>";
   echo '<input type="radio" name="estado" value="'.$reg["estado_id"].'">'.$reg["estado"];
echo "</label>";
   }
?>
 </fieldset>
<br/>

<label> Ingrese Fecha inicial de la sancion</label>
  <div> 
  <script>
  $( function() {
    $( "#fecha1" ).datepicker();
  } );
  </script>

<input type="text" name="fecha1" id="fecha1"></p>
 </div>

   <label> Ingrese Fecha Final de la sancion</label>
<div> 
    <script>
  $( function() {
    $( "#fecha2" ).datepicker();
  } );
  </script>
  <input type="text" name="fecha2" id="fecha2"></p>
 </div> 
    <input type="submit" value="enviar">

     <label> Ingrese observacion </label>
    <input type="text" id="observacion" name="observacion"><br/>



</form>
</body>
</html>
    
asked by Victor Alvarado 19.01.2017 в 04:56
source

1 answer

3

In your code I can see errors, such as:

  • Within <select id="articulo" :
    • You have a <script> tag
    • You print a tag <input id='descripcion'
  • The query to get the article description is missing you from passing the id (eg: SELECT des_articulo FROM articulos WHERE id_articulo=id_articulo , this is wrong)
  • Your script to get the " description of the article " selected in <select id="articulo" is wrong (eg: $('#descripcion').val($(this).val()); , is wrong).

Solution:

  • Remove from the <select id="articulo" tag what does not go.
  • Create a variable, for example, $descripciones to save the descriptions.
  • Within the while ($reg = mysqli_fetch_array($registros)) { add to the variable $descripciones a tag, with id unique and with hidden style, where we will save the description of the article (eg: des_articulo )
  • Print the value of variable $descripciones , outside of select
  • Modify the script so that in change of select search the description within the tag with id unique.

So for example:

<?php
include "conexion.php";
global $cone;
?>
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>

    <title> </title>
</head>
<body>
  <h1 align ="center">Bienvenido</h1>
  <a href ="index.html"><ol>Pagina Principal</ol></a>
  <a href ="registrar.php"><ol>Registrar</ol></a>
  <a href ="listado.php"><ol>Listado</ol></a>
  <a href ="Sancion.php"><ol>Sancion</ol></a>
  <form method="POST" action ="procesar4.php">
    <label> Ingrese cedula del sancionado</label>
    <input type="text" id="cedula" name="cedula1"><br/>

    <label> Ingrese cedula del sancionador</label>
    <input type="text" id="cedula" name="cedula2"><br/>

    <label> Ingrese tipo de sancion </label>
    <div>
      <select name="sancion">
<?php
  $registros = mysqli_query($cone, "select * from sanciones");
  while ($reg = mysqli_fetch_array($registros)) {
      echo "<option value='$reg[id_sancion]'>" . "$reg[sancion]" . "<br/>" . "</option>";
  }
?>
      </select>
    </div>
    <label> Ingrese articulo inflingido </label>
    <div>
      <select name="articulo" id="articulo">
        <option>Seleccione un articulo</option>
<?php
      $registros = mysqli_query($cone, "SELECT * FROM articulos");
      $descripciones = '';
      while ($reg = mysqli_fetch_array($registros)) {
          echo "<option value='$reg[id_articulo]'>" . $reg['articulo'] . "</option>";
          $descripciones .= "<textarea id='desc".$reg['id_articulo']."' style='display: none;'>".$reg['des_articulo']."</textarea>";
      }
?>
      </select>
<?php
      echo $descripciones;
?>
    </div>
    <br/>
    <input type='text' id='descripcion' readonly placeholder="Seleccione un articulo" />
    <script type="text/javascript">
    $(function() {
      // Change es un evento que se ejecuta cada vez que se cambia el valor de un elemento (input, select, etc).
      $('#articulo').change(function(e) {
        $('#descripcion').val($('#desc' + this.value).val());
      }).trigger('change');
    });
    </script>

    <fieldset>
      <legend>Ingrese estado de sancion</legend>
<?php
  $registros = mysqli_query($cone, "select * from estado_sanciones");
  while ($reg = mysqli_fetch_array($registros)) {
      echo "<label>";
      echo '<input type="radio" name="estado" value="' . $reg["estado_id"] . '">' . $reg["estado"];
      echo "</label>";
  }
?>
 </fieldset>
  <br/>

  <label> Ingrese Fecha inicial de la sancion</label>
    <div>
      <script>
      $( function() {
        $( "#fecha1" ).datepicker();
      } );
      </script>
      <input type="text" name="fecha1" id="fecha1"></p>
    </div>
    <label> Ingrese Fecha Final de la sancion</label>
    <div>
      <script>
      $( function() {
        $( "#fecha2" ).datepicker();
      } );
      </script>
      <input type="text" name="fecha2" id="fecha2"></p>
    </div>
    <input type="submit" value="enviar">

    <label> Ingrese observacion </label>
    <input type="text" id="observacion" name="observacion"><br/>
  </form>
</body>
</html>
    
answered by 19.01.2017 / 18:31
source