
Ajax no hace la llamada a php
#1
Escrito 01 noviembre 2012 - 02:37
[js]
<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>
Gracias y un saludo
[/js]
#2
Escrito 01 noviembre 2012 - 03:23
#3
Escrito 01 noviembre 2012 - 03:28
No me da ningún error simplemente no hace nada.
Te pongo todo:
<!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
/* 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
#4
Escrito 01 noviembre 2012 - 03:38
$res = mysql_query("select * from usuarios where telefono='".$_GET['telefono']."'")
#5
Escrito 01 noviembre 2012 - 03:50
He modificado esta linea que tenia un error.
$res = mysql_query("select * from usuarios where telefono='".$_GET['telefono']."'")
con eso se ha solucionado tu problema?
#6
Escrito 01 noviembre 2012 - 03:55
Mira te dejo este enlace donde hay un video ejemplo de autocompletar.Tendrias un ejemplo?
http://www.frogueros...input-dinamico/
#7
Escrito 01 noviembre 2012 - 05:36
No se ha arreglado.
Mira te dejo este enlace donde hay un video ejemplo de autocompletar.Tendrias un ejemplo?
http://www.frogueros...input-dinamico/
Trataré de darme tiempo el fin de semana para redactarte un tuto en mi blog
#8
Escrito 04 noviembre 2012 - 11:11
Un saludo
#9
Escrito 05 noviembre 2012 - 08:48
Poliburro has podido hacer el tutorial?
Un saludo
No amigo... mi fin de semana estuvo ocupadisimo... Veo que haces el window.onload = initFormEvents; podrías decirme si el camop teléfono recibe el cursor al cargar la página? has revisado que no marque error en la barra de estado del navegador?
#10
Escrito 05 noviembre 2012 - 09:11
#11
Escrito 05 noviembre 2012 - 09:22
Hola poliburro, no me marca nigún error!
modifica la función de esta manera y refresca la página. ME dices si aparece el alert
[java]
function initFormEvents() {
document.getElementById('telefono').onblur = getClientData;
document.getElementById('telefono').focus();
alert("El formulario ha sido inicializado.");
}
[/java]
#12
Escrito 05 noviembre 2012 - 09:53

#13
Escrito 05 noviembre 2012 - 10:04
#14
Escrito 05 noviembre 2012 - 10:35
Código JAVA
function initFormEvents() {
document.getElementById('telefono').onblur = getClientData;
document.getElementById('telefono').focus();
alert("El formulario ha sido inicializado.");
}[/html5]
Poliburrro, he modificado y al refrescar me da el alert("El formulario ha sido inicializado.");
#15
Escrito 05 noviembre 2012 - 11:11
en la función getclientdata coloca después de la siguiente línea
if(telefono.length==9 && telefono!=currenttelefono){
alert (telefono);
y cuéntanos si despúes de capturar el teléfono te muestra algo
#16
Escrito 05 noviembre 2012 - 11:40
Tambien te pongo la petición de red.
Luego lo he puesto aquí y:
Archivos adjuntos
#17
Escrito 06 noviembre 2012 - 11:15
Un saludo
EDITO: El Firebug me da error: ReferenceError: sack is not defined var ajax = new sack();
#18
Escrito 21 noviembre 2012 - 05:34
Re: Ajax no hace la llamada a php
« Respuesta #5 en: 01 de Noviembre de 2012, 15:55:57 »
Citar
No se ha arreglado.
Mira te dejo este enlace donde hay un video ejemplo de autocompletar.Tendrias un ejemplo?
http://www.frogueros...input-dinamico/
Un saludo
#19
Escrito 22 noviembre 2012 - 10:07
Hola Poliburro, quería saber si ha podido hacer al final el tutorial:
Re: Ajax no hace la llamada a php
« Respuesta #5 en: 01 de Noviembre de 2012, 15:55:57 »
Citar
No se ha arreglado.
Mira te dejo este enlace donde hay un video ejemplo de autocompletar.Tendrias un ejemplo?
http://www.frogueros...input-dinamico/
Un saludo
Saludos amigo... no he conseguido el tiempo pero este fin des semana lo hago... lo anuncaré aquí el lunes.... saludos
#20
Escrito 22 noviembre 2012 - 10:37
