Ver Mensaje Individual
  #5 (permalink)  
Antiguo 01/08/2013, 15:09
Avatar de exmatuz
exmatuz
 
Fecha de Ingreso: mayo-2013
Mensajes: 9
Antigüedad: 10 años, 11 meses
Puntos: 1
Respuesta: Autocomplete de jquery y el valor que recibo, necesito id en vez del nombr

Yo lo logré de esta manera, no se si ya tu lo resolviste

Search.php

Código PHP:
Ver original
  1. <?php
  2.  
  3. if (isset($_GET['term'])){
  4.     $return_arr = array();
  5.  
  6.     try {
  7.         $conn = new PDO('mysql:host=localhost;dbname=db_saga', 'exal', '1990');
  8.         $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  9.  
  10.         $stmt = $conn->prepare("SELECT id_paciente, concat_ws(' ', nombre, apepat, apemat) AS paciente FROM tblc_paciente WHERE concat_ws(' ', nombre, apepat, apemat) LIKE :term");
  11.         $stmt->execute(array('term' => '%'.$_GET['term'].'%'));
  12.  
  13.         while($row = $stmt->fetch()) {
  14.  
  15.            $return_arr[] = array('value' => $row['paciente'], 'id' => $row['id_paciente']);
  16.  
  17.         }
  18.  
  19.     } catch(PDOException $e) {
  20.         echo 'ERROR: ' . $e->getMessage();
  21.     }
  22.  
  23.     header('Content-type: application/json');
  24.     /* Toss back results as json encoded array. */
  25.     echo json_encode($return_arr);
  26. }
  27.  
  28. ?>

index.html

Código HTML:
Ver original
  1.     <meta charset="UTF-8">
  2.     <title>Document</title>
  3.     <link rel="stylesheet" href="css/jquery-ui-1.10.3.custom.min.css">
  4.     <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
  5.     <script type="text/javascript" src="js/jquery-ui-1.10.3.custom.min.js"></script>
  6.     <script type="text/javascript">
  7.     $(function() {
  8.             //autocomplete
  9.             $(".auto").autocomplete({
  10.                 source: "search.php",
  11.                 minLength: 1,
  12.                 select: function(event, data) {
  13.                     $("#id_paciente").val(data.item.id);
  14.                     $(".auto").val(data.item.value);
  15.                 },
  16.             });
  17.         });
  18.  
  19.     </script>
  20. </head>
  21.     <form method="post" action="insert_paciente.php">
  22.         <p><label>paciente:</label><input type='text' id='title' name='title' value='' class='auto'></p>
  23.         <p>Inicio de cita:<label></label><input type='text' id='inicio' name='inicio'></p>
  24.         <p><label>Fin de cita:</label><input type='text' id='fin' name='fin'></p>
  25.         <p><label>Status:</label><input type='text' id='status' name='status'></p>
  26.         <input  type='hidden' id='id_paciente' name='id_paciente'></p>
  27.         <p><label>doctor:</label><input  type='text' id='id_trabajador' name='id_trabajador'></p>
  28.         <input type="submit">
  29.     </form>
  30.  
  31. </body>
  32. </html>

en el input .auto hago la busqueda del nombre, al seleccionar el nombre que busco, y su id se manda a un input hidden.