Problemas con autocompletar en formulario PHP

  • Autor Autor benzema
  • Fecha de inicio Fecha de inicio
B

benzema

Curioso
¡Usuario con pocos negocios! ¡Utiliza siempre saldo de Forobeta!
Buenos días, tengo este autocompletar y no me funciona. Por favor podéis decirme donde está el fallo:


PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Documento sin título</title>

<style type="text/css">

 

   body{

 

      background-repeat:no-repeat;

 

      font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif;

 

      height:100%;

 

      background-color: #FFF;

 

      margin:0px;

 

      padding:0px;

 

      background-image:url('/images/heading3.gif');

 

      background-repeat:no-repeat;

 

      padding-top:85px;

 

   }

 

   

 

   fieldset{

 

      width:500px;

 

      margin-left:10px;

 

   }

 

 

 

   </style>

 

    <script type="text/javascript" src="js/ajax.js"></script>

   <script type="text/javascript">

   

   var ajax = new sack();

 

   var currenttelefono=false;

 

   function getClientData()

 

   {

 

      var telefono = document.getElementById('telefono').value.replace(/[^0-9]/g,'');

 

      if(telefono.length==9 && telefono!=currenttelefono){

 

         currenttelefono = telefono

 

         ajax.requestFile = 'getClient.php?gettelefono=' telefono;   // Specifying which file to get

 

         ajax.onCompletion = showClientData;   // Specify function that will be executed after file has been found

 

         ajax.runAJAX();      // Execute AJAX function         

 

      }

 

      

 

   }

 

   

 

   function showClientData()

 

   {

 

      var formObj = document.forms['clientForm'];   

 

      eval(ajax.response);

 

   }

 

   

 

   

 

   function initFormEvents()

 

   {

 

      document.getElementById('telefono').onblur = getClientData;

 

      document.getElementById('telefono').focus();

 

   }

 

   window.onload = initFormEvents;

 

   </script>

</head>

 

<body>

<form name="clientForm" action="ajax-client_lookup.html" method="post">   

 

   <fieldset>

 

      <legend>Client information</legend>

 

      <table>

 

         <tr>

 

            <td><label for="telefono">Nº Teléfono:</label></td>

 

            <td><input name="telefono" id="telefono" size="9" maxlength="9"></td>

 

         </tr>

 

         <tr>

 

            <td><label for="nombre">First name:</label></td>

 

            <td><input name="nombre" id="nombre" size="20" maxlength="255"></td>

 

         </tr>

 

         <tr>

 

            <td><label for="apellidos">Last name:</label></td>

 

            <td><input name="apellidos" id="apellidos" size="20" maxlength="255"></td>

 

         </tr>

 

      </table>   

 

   </form>

 

   </fieldset>

 

 

</body>

</html>


getClient.php


PHP:
<?php

/* Replace the data in these two lines with data for your db connection */

$connection = mysql_connect("localhost","root","root");  

mysql_select_db("pruebas",$connection);

 

if(isset($_GET['gettelefono'])){  

  $res = mysql_query("select * from usuarios where telefono='".$_GET['gettelefono']."'") or die(mysql_error());

  if($inf = mysql_fetch_array($res)){

    echo "formObj.nombre.value = '".$inf["nombre"]."';\n";    

    echo "formObj.apellidos.value = '".$inf["apellidos"]."';\n";    

    

  }else{

    echo "formObj.nombre.value = '';\n";    

    echo "formObj.apellidos.value = '';\n";    

  

  }    

}

?>

Gracias y un saludo
 
abriste directamente el getClient.php? hazle echo a ver si funciona nunca había usado ajax así...
 
heyfranks, gracias por contestar pero no tengo muchos conocimientos en cuanto a programación y no sé a lo que te refieres con:
Insertar CODE, HTML o PHP:
hazle echo a ver si funciona nunca había usado ajax así...
Yo no veo ningún fallo en el getClient.php.
Un saludo
 
heyfranks, gracias por contestar pero no tengo muchos conocimientos en cuanto a programación y no sé a lo que te refieres con:
Insertar CODE, HTML o PHP:
hazle echo a ver si funciona nunca había usado ajax así...
Yo no veo ningún fallo en el getClient.php.
Un saludo
abres directamente el getClient.php y te deberia decir

echo "formObj.nombre.value = '".$inf["nombre"]."';\n";
echo "formObj.apellidos.value = '".$inf["apellidos"]."';\n";

Si te lanza un error, es por base de datos ya que en el js no veo error superficialmente a escepcion de currenttelefono = telefono que le falta un ;
 
Otra vez gracias por responder.
He hecho otro cod y explico que quiero hacer:
He hecho un formulario en el cual tengo varios input donde recojo datos. En el primer input name="telefono" hago un select con una lista donde selecciono el dato que quiero.Y me pregunta es como puedo recuperar mas variables para mas input?

Quiero integrar una cosa así en el cod php: document.getElementById('apellidos').value='<?=$ro w['apellidos']; ?>' en el id="apellidos" del formulario.


html

PHP:
<div class="form">
<form action="index_ajax.php" name"form1" method="get">
<div class="ausu-suggest">
<input type="text" size="9" value="" name="telefono" id="telefono" autocomplete="off" />
<input type="text" size="30" value="" name="apellidos" id="apellidos" autocomplete="off" */>
<input type="text" size="30" value="" name="poblacion" id="poblacion" autocomplete="off" */>
</form>
</div>
 </div>


php

PHP:
$result = mysql_query($query); 

        $dataList = array(); 

        while ($row = mysql_fetch_array($result)) 
        { 
        $toReturn   = $row['telefono']; 
        $dataList[] = '<li><a href="#">' . htmlentities($toReturn) . '</a></li>';      
        } 
     if (count($dataList)>=1) 
     
        {  
            $dataOutput = join("\r\n", $dataList); 
            echo $dataOutput; 
             
        } 
        else 
        { 
            echo '<li><a href="#">No Results</a></li>'; 
        } 
} 
} 
?>

Gracias y un saludo
 
Última edición:
Atrás
Arriba