Ver Mensaje Individual
  #3 (permalink)  
Antiguo 19/05/2011, 05:36
ramiromd
 
Fecha de Ingreso: agosto-2010
Mensajes: 100
Antigüedad: 13 años, 8 meses
Puntos: 2
Respuesta: Primeros pasos con Xajax, no hago funcionar una lista dependiente

aniMAYtions gracias por la respuesta. Te comento, empecé a hacer un código propio. Con otro ejemplo que no son provincias y estados, sino países y equipos de fútbol.

Mis código son estos:
Generador del select de equipos
Código PHP:
Ver original
  1. function getEquipo($id_pais){
  2.     $respuesta = new xajaxResponse('ISO-8859-1');
  3.     $query = "SELECT e.idequipo, e.nombre FROM Equipo e WHERE e.idPais = $id_pais";
  4.     $res = mysql_query($query);
  5.     $new_select = "<select name='equipo'>";
  6.     while ($row = mysql_fetch_array($res)){
  7.         $new_select .= "<option value='".$row[0]."'>".htmlentities($row[1])."</option>";
  8.     }
  9.     $new_select .= "</select>";
  10.     $respuesta->addAssign("equipoDiv","innerHTML",$new_select);
  11.     return $respuesta;
  12. }

Head del html
Código HTML:
Ver original
  1.     <title>Select dependientes - Xajax!</title>
  2.     <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  3.     <meta name="generator" content="Geany 0.19.1" />
  4.     <?php
  5.         include('xajax/xajax.inc.php');
  6.         $xajax = new xajax();
  7.         $xajax->setCharEncoding('ISO-8859-1');
  8.         $xajax->decodeUTF8InputOn();
  9.         include('bd.php');
  10.         conectar();
  11.         //asociamos la función creada anteriormente al objeto xajax
  12.         $xajax->registerFunction("getEquipo");
  13.         //El objeto xajax tiene que procesar cualquier petición
  14.         $xajax->processRequests();
  15.         $xajax->printJavascript("xajax/");
  16.      ?>
  17. </head>

Form en cuestión.
Código HTML:
Ver original
  1. <form name="pais_equipo">
  2.     <select name="pais" onchange="xajax_getEquipo(this.value)">
  3.         <?php getPais(); ?>
  4.     </select>
  5.     <div id="equipoDiv">
  6.     <select name="equipo">
  7.             <option value="999">Elije un pa&iacute;s</option>
  8.     </select>
  9.     </div>
  10. </form>

No sé si me estará faltando algun adeclaración. Ando medio perdido, pero al seleccionar un país e intentar pinchar el select de equipos me devuelve el siguiente error a modo de alert():

Error: the XML response that was returned from the server is invalid.
Received:
Cita:
<!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" xml:lang="en" lang="en">

<head>
<title>Select dependientes - Xajax!</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="generator" content="Geany 0.19.1" />
<?xml version="1.0" encoding="ISO-8859-1" ?><xjx><cmd n="as" t="equipoDiv" p="innerHTML"><![CDATA[<select id='equipo'><option
value='16'>Manchester City</option><option value='17'>Manchester United</option><option value='18'>Tottenham Hotspurs</
option><option value='19'>Sunderland</option></select>]]></cmd>
</xjx>
Al parecer la consulta se genera bien, pero no entiendo por qué no genera el nuevo select :(