Foros del Web » Programando para Internet » Javascript » Frameworks JS »

Problema con menu desplegable Ajax

Estas en el tema de Problema con menu desplegable Ajax en el foro de Frameworks JS en Foros del Web. Tengo el siguiente codigo en el cual me muestra todo correcto excepto cuando se selecciona una opcion la cual no hay nada que mostrar muestra ...
  #1 (permalink)  
Antiguo 07/07/2008, 16:57
Avatar de Highlander  
Fecha de Ingreso: junio-2006
Ubicación: Concepción, Chile
Mensajes: 475
Antigüedad: 17 años, 11 meses
Puntos: 4
Problema con menu desplegable Ajax

Tengo el siguiente codigo en el cual me muestra todo correcto excepto cuando se selecciona una opcion la cual no hay nada que mostrar muestra un valor incorrecto, imagino que sera algun error de novato.

archivo combo.php

Código:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "xhtml1-transitional.dtd">
<html>
<head>
<title>Sentido Web - Combo AJAX</title>
<script type="text/javascript">
// <![CDATA[
function ajaxobj() {
	try {
		_ajaxobj = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			_ajaxobj = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			_ajaxobj = false;
		}
	}
   
	if (!_ajaxobj && typeof XMLHttpRequest!='undefined') {
		_ajaxobj = new XMLHttpRequest();
	}
	
	return _ajaxobj;
}

function carga(val) {
	var ajax = ajaxobj();
	ajax.open("POST", "ajax.php", true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			var datos = (ajax.responseXML).firstChild;
			var obj = document.getElementById("combo2");
			for (var i=0; i<obj.options.length; i++) {
        obj.removeChild(obj.firstChild);
      }
      for (var i=0; i<datos.childNodes.length; i++) {
        var elem = datos.childNodes[i].firstChild.data;
        obj.options[i] = new Option(elem);
      }
		}
	}
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send("&val="+val);
}

// ]] >
</script>
</head>
<body >
<p>

		<?php 
				   include("conexion.php");
				   $link=conectar();
			   	   $consulta = "SELECT * FROM ciudad ORDER BY NOM_CIUDAD"; 
			       $resultado = mysql_query($consulta,$link);
				   
		?>
            <select name="id_ciudad"  id="id_ciudad" onchange="carga(this.value)"> 
              <option value="" selected="selected">Seleccionar</option>
              <?php
					while($datos = mysql_fetch_array($resultado)){
					?>
              <option value="<?php echo $datos["ID_CIUDAD"] ?>"><?php echo $datos["NOM_CIUDAD"] ?></option>
              <?php
					}
					mysql_close($link);
			 		?>
            </select>


<select id="combo2">
  <option value="">Vacio</option>
</select>
</p>
</body>
</html>
Archivo ajax.php

Código:
<?php

	$val = $_POST["val"];
    $id_esta = array();
	$nom_esta = array();
	
	include("conexion.php");
	$link=conectar();
	$consulta = "SELECT * FROM establecimiento WHERE id_ciudad = '$val' ORDER BY id_esta"; 
	$resultado = mysql_query($consulta,$link);
	$j=0;
	while($datos = mysql_fetch_array($resultado)){
	$id_esta[]=$datos["ID_ESTA"];
	$nom_esta[]=$datos["NOM_ESTA"];
	$j++;
	} 
	mysql_close($link);
	
	$xml .= '<datos>';
    for ($i=0; $i<=$j; $i++){
    $xml .= '<valor>'.$nom_esta[$i].'</valor>'; 
	}
	$xml .= '</datos>';
	header('Content-type: text/xml');
	echo $xml;	
	 
	 
	/* $xml .= '<datos>';
  for ($i=1; $i<=5; $i++) {
    $xml .= '<valor>'.$val.'</valor>'; 
	}
	$xml .= '</datos>';
	header('Content-type: text/xml');
	echo $xml;	 */
?>
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:19.