Problemas con Ajax, php y mysql
Publicado por Daniel (2 intervenciones) el 21/09/2005 01:15:14
Buenas, antes de nada decir que pongo esta nota aqui xq todavia no hay ningun foro sobre Ajax.
Hace poco saque un ejemplo sobre Ajax y estube practicando con el, el problema es que no me funciona como deberia o no me funciona.
El ejemplo de la pagina html es la siguiente:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Ciudad del Codigo Postal usando XmlHttpRequest</title>
<script language="javascript" type="text/javascript">
var url = "getCityState.php?param="; // La direccion del archivo .php y el parametro que se le pasa
var http = getHTTPObject(); // Esto crea el Objeto HTTP
var enProceso = false; //Variable para saber si existe otro proceso en ejecucion
function handleHttpResponse() {
if (http.readyState == 4) {
if (http.responseText.indexOf('invalid') == -1) {
// Use the XML DOM to unpack the city and state data
var xmlDocument = http.responseXML;
var city = xmlDocument.getElementsByTagName('city').item(0).firstChild.data;
var state = xmlDocument.getElementsByTagName('state').item(0).firstChild.data;
document.getElementById('city').value = "yo"; //city;
document.getElementById('state').value = "tu"; //state;
enProceso = false;
}
/*results = http.responseText.split(",");
document.getElementById('city').value = results[0];
document.getElementById('state').value = results[1];
enProceso = false;*/
//document.write ('da igual');
}
}
function updateCityState() {
if (!enProceso && http) {
var zipValue = document.getElementById("zip").value;
http.open ("GET", url + escape(zipValue), true);
http.onreadystatechange = handleHttpResponse;
enProceso = true;
http.send(null);
}
}
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
xmlhttp.overrideMimeType("text/xml");
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
</script>
</head>
<body>
<form action="post">
<p>
ZIP code:
<input type="text" size="5" name="zip" id="zip" onblur="updateCityState();" />
</p>
City:
<input type="text" name="city" id="city" />
State:
<input type="text" size="2" name="state" id="state" />
</form>
</body>
</html>
Parece que todo esta bien, miro varias veces el codigo del ejemplo con el mio y es exactamente igual, pero donde me falla (creo) es en la funcion "handleHttpResponse()" en el sitio donde esta la sentencia "if (http.responseText.indexOf('invalid') == -1)" donde nunca entra, por lo que no muestra las variables pasadas por el archivo php.
Ayudenme que ya estoy un poco desesperado. Gracias a todos.
Hace poco saque un ejemplo sobre Ajax y estube practicando con el, el problema es que no me funciona como deberia o no me funciona.
El ejemplo de la pagina html es la siguiente:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Ciudad del Codigo Postal usando XmlHttpRequest</title>
<script language="javascript" type="text/javascript">
var url = "getCityState.php?param="; // La direccion del archivo .php y el parametro que se le pasa
var http = getHTTPObject(); // Esto crea el Objeto HTTP
var enProceso = false; //Variable para saber si existe otro proceso en ejecucion
function handleHttpResponse() {
if (http.readyState == 4) {
if (http.responseText.indexOf('invalid') == -1) {
// Use the XML DOM to unpack the city and state data
var xmlDocument = http.responseXML;
var city = xmlDocument.getElementsByTagName('city').item(0).firstChild.data;
var state = xmlDocument.getElementsByTagName('state').item(0).firstChild.data;
document.getElementById('city').value = "yo"; //city;
document.getElementById('state').value = "tu"; //state;
enProceso = false;
}
/*results = http.responseText.split(",");
document.getElementById('city').value = results[0];
document.getElementById('state').value = results[1];
enProceso = false;*/
//document.write ('da igual');
}
}
function updateCityState() {
if (!enProceso && http) {
var zipValue = document.getElementById("zip").value;
http.open ("GET", url + escape(zipValue), true);
http.onreadystatechange = handleHttpResponse;
enProceso = true;
http.send(null);
}
}
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
xmlhttp.overrideMimeType("text/xml");
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
</script>
</head>
<body>
<form action="post">
<p>
ZIP code:
<input type="text" size="5" name="zip" id="zip" onblur="updateCityState();" />
</p>
City:
<input type="text" name="city" id="city" />
State:
<input type="text" size="2" name="state" id="state" />
</form>
</body>
</html>
Parece que todo esta bien, miro varias veces el codigo del ejemplo con el mio y es exactamente igual, pero donde me falla (creo) es en la funcion "handleHttpResponse()" en el sitio donde esta la sentencia "if (http.responseText.indexOf('invalid') == -1)" donde nunca entra, por lo que no muestra las variables pasadas por el archivo php.
Ayudenme que ya estoy un poco desesperado. Gracias a todos.
Valora esta pregunta


0