Advertisement
kumatron

Untitled

Jan 3rd, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. /* I'm not able to get it working after editing the code. What I did was changing the id y name of all the fields and php code.
  2. I.E. nombre replaced city, so in each appearence of city, I changed it.
  3. Is this incorrect? Am I missing something?
  4.  
  5. This is my HTML: */
  6.  
  7.  
  8. <script type="text/javascript">
  9.  
  10. jQuery(document).ready(function(){
  11.     var ac_config = {
  12.         source: "datos.php",
  13.         select: function(event, ui){
  14.             jQuery("#nrocliente").val(ui.item.nrocliente);
  15.             jQuery("#nombre").val(ui.item.nombre);
  16.             jQuery("#apellido").val(ui.item.apellido);
  17.             jQuery("#mensaje").val(ui.item.mensaje);
  18.         },
  19.         minLength:1
  20.     };
  21.     jQuery("#nombre").autocomplete(ac_config);
  22. });
  23. </script>
  24.  
  25. <form method="post" action="/">
  26. <input type="hidden" name="page_id" id="page_id" value="12" />
  27.  
  28. <label for="nombre">Nombre:</label> <input type="text" name="nombre" id="nombre" value="" /><br />
  29.  
  30. <label for="apellido">Apellido:</label> <input type="text" name="apellido" id="apellido" value="" /><br />
  31.  
  32. <label for="mensaje">Mensaje:</label> <input type="text" name="mensaje" id="mensaje" value="" /><br />
  33.  
  34. <input type="hidden" name="nrocliente" id="nrocliente" value="" />
  35. <input type="submit" value="Enviar" /><br />
  36. </form>
  37.  
  38.  
  39. // And this is my datos.php file:
  40.  
  41.  
  42. <?php
  43. // Wordpress database call through $wpdb
  44.  $clientes = $wpdb->get_results('SELECT id AS nrocliente, nombre, apellido, mensaje FROM clientes');
  45.  
  46.  // Turns the array of objects into an array of arrays, exactly like the example
  47.  
  48. $array =  array();
  49. foreach($clientes as $cliente){
  50.     $arrcli =  (array) $cliente;
  51.     $array[] = $arrcli;
  52.     }
  53.  
  54. //print_r($array);
  55.  
  56. // Cleaning up the term
  57. $term = trim(strip_tags($_GET['term']));
  58.  
  59. // Rudimentary search
  60. $matches = array();
  61. foreach($array as $data){
  62.     if(stripos($data['nombre'], $term) !== false){
  63.         // Add the necessary "value" and "label" fields and append to result set
  64.         $data['value'] = $data['nombre'];
  65.         $data['label'] = "{$data['nombre']}, {$data['apellido']} {$data['mensaje']}";
  66.         $matches[] = $data;
  67.     }
  68. }
  69.  
  70. // Truncate, encode and return the results
  71. $matches = array_slice($matches, 0, 5);
  72. print json_encode($matches);
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement