Ver Mensaje Individual
  #13 (permalink)  
Antiguo 19/10/2012, 10:15
Avatar de satjaen
satjaen
 
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Php para Autocompletar

Ok,

Autocompletar.php

Código Javascript:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.  
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Ajax Auto Suggest</title>
  7.  
  8. <script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
  9. <script type="text/javascript">
  10.     function lookup(inputString) {
  11.         if(inputString.length == 0) {
  12.             // Hide the suggestion box.
  13.             $('#suggestions').hide();
  14.         } else {
  15.             $.post("rpc.php", {queryString: ""+inputString+""}, function(data){
  16.                 if(data.length >0) {
  17.                     $('#suggestions').show();
  18.                     $('#autoSuggestionsList').html(data);
  19.                 }
  20.             });
  21.         }
  22.     } // lookup
  23.    
  24.     function fill(thisValue) {
  25.         $('#inputString').val(thisValue);
  26.         setTimeout("$('#suggestions').hide();", 200);
  27.     }
  28. </script>
  29.  
  30. <style type="text/css">
  31.     body {
  32.         font-family: Helvetica;
  33.         font-size: 11px;
  34.         color: #000;
  35.     }
  36.    
  37.     h3 {
  38.         margin: 0px;
  39.         padding: 0px;  
  40.     }
  41.  
  42.     .suggestionsBox {
  43.         position: relative;
  44.         left: 30px;
  45.         margin: 10px 0px 0px 0px;
  46.         width: 200px;
  47.         background-color: #212427;
  48.         -moz-border-radius: 7px;
  49.         -webkit-border-radius: 7px;
  50.         border: 2px solid #000;
  51.         color: #fff;
  52.     }
  53.    
  54.     .suggestionList {
  55.         margin: 0px;
  56.         padding: 0px;
  57.     }
  58.    
  59.     .suggestionList li {
  60.        
  61.         margin: 0px 0px 3px 0px;
  62.         padding: 3px;
  63.         cursor: pointer;
  64.     }
  65.    
  66.     .suggestionList li:hover {
  67.         background-color: #659CD8;
  68.     }
  69. </style>
  70.  
  71. </head>
  72.  
  73. <body>
  74.  
  75.  
  76.     <div>
  77.         <form>
  78.             <div>
  79.                 Selecciona el Nº de teléfono:
  80.                 <br />
  81.                 <input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" />
  82.             </div>
  83.            
  84.             <div class="suggestionsBox" id="suggestions" style="display: none;">
  85.                 <img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
  86.                 <div class="suggestionList" id="autoSuggestionsList">
  87.                     &nbsp;
  88.                 </div>
  89.             </div>
  90.         </form>
  91.     </div>
  92.  
  93. </body>
  94. </html>


rpc.php


Código PHP:
<?php
    
    
// PHP5 Implementation - uses MySQLi.
    // mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
    
$db = new mysqli('localhost''root' ,'root''xxxxx');
    if (
$mysqli->connect_error) {
    die(
'Error de Conexión (' $mysqli->connect_errno ') '
            
$mysqli->connect_error);
}

/*
 * Use esto en lugar de $connect_error si necesita asegurarse
 * de la compatibilidad con versiones de PHP anteriores a 5.2.9 y 5.3.0.
 */
if (mysqli_connect_error()) {
    die(
'Error de Conexión (' mysqli_connect_errno() . ') '
            
mysqli_connect_error());
}

echo 
'Éxito... ' $mysqli->host_info "\n";
    
    if(!
$db) {
        
// Show error if we cannot connect.
        
echo 'ERROR: Could not connect to the database.';
    } else {
        
// Is there a posted query string?
        
if(isset($_POST['queryString'])) {
            
$queryString $db->real_escape_string($_POST['queryString']);
            
            
// Is the string length greater than 0?
            
            
if(strlen($queryString) >0) {
                
// Run the query: We use LIKE '$queryString%'
                // The percentage sign is a wild-card, in my example of countries it works like this...
                // $queryString = 'Uni';
                // Returned data = 'United States, United Kindom';
                
                // YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
                // eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10
                
                
$query $db->query("SELECT telefono FROM usuarios WHERE telefono LIKE '$queryString%' LIMIT 10");
                if(
$query) {
                    
// While there are results loop through them - fetching an Object (i like PHP5 btw!).
                    
while ($result $query ->fetch_object()) {
                        
// Format the results, im using <li> for the list, you can change it.
                        // The onClick function fills the textbox with the result.
                        
                        // YOU MUST CHANGE: $result->value to $result->your_colum
                         
echo '<li onClick="fill(\''.$result->value.'\');">'.$result->value.'</li>';
                     }
                } else {
                    echo 
'ERROR: There was a problem with the query.';
                }
            } else {
                
// Dont do anything.
            
// There is a queryString.
        
} else {
            echo 
'There should be no direct access to this script!';
        }
    }
?>
Saludos.