el problema . cuando seleciona un pais filtra los destino depende al pais selecionado. ahi va bien
cuando selecciona cadena. para que filtre.
seleciona cadena mientras. que pais = y destino = .
le metido varible arriba abajo al final. pero nada
me dado cuenta. que cuando seleciono la primera crea la variable $val. cuando seleciono el segundo combo box la varible se imprimir pero . cambia de pais a destino. se pieder. e tratodo de mantener. pero no nada me resulta,
Aqui dejo el codigo completo.
file:state.php
Citar
<?
//set IE read from page only not read from cache
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header("content-type: application/x-javascript; charset=tis-620");
$data=$_GET['data'];
$val=$_GET['val'];
//set database
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "147";
$dbname = "test";
mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server");
if ($data=='pais') { // first dropdown
echo "<select name='pais' onChange=\"dochange('destino', this.value)\">\n";
echo "<option value='0'>==== Selecione Pais ====</option>\n";
$result=mysql_db_query($dbname,"select `id`, `paisname` , `codigo3` from pais order by `paisname`");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
} else if ($data=='destino') { // second dropdown
echo "<select name='destino' onChange=\"dochange('cadena', this.value)\">\n";
echo "<option value='0'>====Selecione Destino ====</option>\n";
$result=mysql_db_query($dbname,"SELECT `id`, `city` FROM destino WHERE `id_pais` = '$val' ORDER BY `city` ");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
}
else if ($data=='cadena') { // second dropdown
echo "<select name='cadena' >\n";
echo "<option value='0'>====choose cities ====</option>\n";
$result=mysql_db_query($dbname,"SELECT `id`, `nombres` FROM cadena WHERE `id_pais` = '$val' AND id_destino '$val' = ORDER BY `id` ");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
}
echo "</select>\n";
?>
//set IE read from page only not read from cache
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header("content-type: application/x-javascript; charset=tis-620");
$data=$_GET['data'];
$val=$_GET['val'];
//set database
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "147";
$dbname = "test";
mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server");
if ($data=='pais') { // first dropdown
echo "<select name='pais' onChange=\"dochange('destino', this.value)\">\n";
echo "<option value='0'>==== Selecione Pais ====</option>\n";
$result=mysql_db_query($dbname,"select `id`, `paisname` , `codigo3` from pais order by `paisname`");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
} else if ($data=='destino') { // second dropdown
echo "<select name='destino' onChange=\"dochange('cadena', this.value)\">\n";
echo "<option value='0'>====Selecione Destino ====</option>\n";
$result=mysql_db_query($dbname,"SELECT `id`, `city` FROM destino WHERE `id_pais` = '$val' ORDER BY `city` ");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
}
else if ($data=='cadena') { // second dropdown
echo "<select name='cadena' >\n";
echo "<option value='0'>====choose cities ====</option>\n";
$result=mysql_db_query($dbname,"SELECT `id`, `nombres` FROM cadena WHERE `id_pais` = '$val' AND id_destino '$val' = ORDER BY `id` ");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
}
echo "</select>\n";
?>
file:state_dropdown.php
Citar
<a href="http://php-ajax-code.blogspot.com/"><img src="images/php_ajax_code.gif" border=0></a><br>
<br>
This is sample Ajax DropDown Menu When you select state in first dropdown menu<br>
the second dropdown menu will change automatic and display cites in the state selection
<br><br>
<?
echo "<form name=sel>\n";
echo "Pais : <font id=pais><select>\n";
echo "<option value='0'>============</option> \n" ;
echo "</select></font>\n";
echo "Destino : <font id=destino><select>\n";
echo "<option value='0'>=== Seleccione ===</option> \n" ;
echo "</select></font>\n";
echo "Cadena : <font id=cadena><select>\n";
echo "<option value='0'>=== Seleccione ===</option> \n" ;
echo "</select></font>\n";
?>
<script language=javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native javascript
alert("XMLHttpRequest not supported");
return null;
};
function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //retuen value
}
}
};
req.open("GET", "state.php?data="+src+"&val="+val); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}
window.onLoad=dochange('pais', -1); // value in first dropdown
</script>
<br><br>source from : <a href="http://php-ajax-code.blogspot.com/">php-ajax-code.blogspot.com</a>
<br>
This is sample Ajax DropDown Menu When you select state in first dropdown menu<br>
the second dropdown menu will change automatic and display cites in the state selection
<br><br>
<?
echo "<form name=sel>\n";
echo "Pais : <font id=pais><select>\n";
echo "<option value='0'>============</option> \n" ;
echo "</select></font>\n";
echo "Destino : <font id=destino><select>\n";
echo "<option value='0'>=== Seleccione ===</option> \n" ;
echo "</select></font>\n";
echo "Cadena : <font id=cadena><select>\n";
echo "<option value='0'>=== Seleccione ===</option> \n" ;
echo "</select></font>\n";
?>
<script language=javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native javascript
alert("XMLHttpRequest not supported");
return null;
};
function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //retuen value
}
}
};
req.open("GET", "state.php?data="+src+"&val="+val); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}
window.onLoad=dochange('pais', -1); // value in first dropdown
</script>
<br><br>source from : <a href="http://php-ajax-code.blogspot.com/">php-ajax-code.blogspot.com</a>
data: test.sql
Citar
-- phpMyAdmin SQL Dump
-- version 2.10.3
-- http://www.phpmyadmin.net
--
-- Servidor: localhost
-- Tiempo de generación: 13-01-2011 a las 11:13:57
-- Versión del servidor: 5.0.51
-- Versión de PHP: 5.2.6
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Base de datos: `test`
--
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `cadena`
--
CREATE TABLE `cadena` (
`id` int(11) NOT NULL auto_increment,
`id_destino` int(11) default NULL,
`id_pais` int(11) default NULL,
`nombres` varchar(20) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
--
-- Volcar la base de datos para la tabla `cadena`
--
INSERT INTO `cadena` VALUES (1, 1, 1, 'tarata');
INSERT INTO `cadena` VALUES (2, 1, 1, 'sama');
INSERT INTO `cadena` VALUES (3, 2, 1, 'erer');
INSERT INTO `cadena` VALUES (4, 2, 1, 'werewr');
INSERT INTO `cadena` VALUES (5, 2, 2, 'oiuouio');
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `destino`
--
CREATE TABLE `destino` (
`id` int(11) NOT NULL auto_increment,
`id_pais` int(11) NOT NULL default '0',
`city` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `id_pais` (`id_pais`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;
--
-- Volcar la base de datos para la tabla `destino`
--
INSERT INTO `destino` VALUES (1, 1, 'Bariloche');
INSERT INTO `destino` VALUES (2, 1, 'Buenos Aires');
INSERT INTO `destino` VALUES (3, 1, 'Calafate');
INSERT INTO `destino` VALUES (4, 1, 'Cordoba');
INSERT INTO `destino` VALUES (5, 1, 'Mar del Plata');
INSERT INTO `destino` VALUES (6, 1, 'Mensoza');
INSERT INTO `destino` VALUES (7, 1, 'Puerto de Iguazu');
INSERT INTO `destino` VALUES (8, 1, 'Puerto Madryn');
INSERT INTO `destino` VALUES (9, 1, 'Ushuaia');
INSERT INTO `destino` VALUES (10, 2, 'Aruba');
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `pais`
--
CREATE TABLE `pais` (
`id` int(11) NOT NULL auto_increment,
`paisname` varchar(255) NOT NULL,
`codigo3` char(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=22 ;
--
-- Volcar la base de datos para la tabla `pais`
--
INSERT INTO `pais` VALUES (1, 'Argentina', '0');
INSERT INTO `pais` VALUES (2, 'Aruba', '0');
INSERT INTO `pais` VALUES (3, 'Bahamas', '0');
INSERT INTO `pais` VALUES (4, 'Bolivia', '0');
INSERT INTO `pais` VALUES (5, 'Brasil', '0');
INSERT INTO `pais` VALUES (6, 'Chile', '0');
INSERT INTO `pais` VALUES (7, 'Colombia', '0');
INSERT INTO `pais` VALUES (8, 'Costa Rica', '0');
INSERT INTO `pais` VALUES (9, 'Cuba', '0');
INSERT INTO `pais` VALUES (10, 'Ecuador', '0');
INSERT INTO `pais` VALUES (11, 'Estados Unidos', '0');
INSERT INTO `pais` VALUES (12, 'Guatemala', '0');
INSERT INTO `pais` VALUES (13, 'Jamaica', '0');
INSERT INTO `pais` VALUES (14, 'Mexico', '0');
INSERT INTO `pais` VALUES (15, 'Nicaragua', '0');
INSERT INTO `pais` VALUES (16, 'Panama', '0');
INSERT INTO `pais` VALUES (17, 'Peru', '0');
INSERT INTO `pais` VALUES (18, 'Puerto rico', '0');
INSERT INTO `pais` VALUES (19, 'Republica Dominicana', '0');
INSERT INTO `pais` VALUES (20, 'Uruguay', '0');
INSERT INTO `pais` VALUES (21, 'Venezuela', '0');
-- version 2.10.3
-- http://www.phpmyadmin.net
--
-- Servidor: localhost
-- Tiempo de generación: 13-01-2011 a las 11:13:57
-- Versión del servidor: 5.0.51
-- Versión de PHP: 5.2.6
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Base de datos: `test`
--
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `cadena`
--
CREATE TABLE `cadena` (
`id` int(11) NOT NULL auto_increment,
`id_destino` int(11) default NULL,
`id_pais` int(11) default NULL,
`nombres` varchar(20) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
--
-- Volcar la base de datos para la tabla `cadena`
--
INSERT INTO `cadena` VALUES (1, 1, 1, 'tarata');
INSERT INTO `cadena` VALUES (2, 1, 1, 'sama');
INSERT INTO `cadena` VALUES (3, 2, 1, 'erer');
INSERT INTO `cadena` VALUES (4, 2, 1, 'werewr');
INSERT INTO `cadena` VALUES (5, 2, 2, 'oiuouio');
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `destino`
--
CREATE TABLE `destino` (
`id` int(11) NOT NULL auto_increment,
`id_pais` int(11) NOT NULL default '0',
`city` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `id_pais` (`id_pais`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;
--
-- Volcar la base de datos para la tabla `destino`
--
INSERT INTO `destino` VALUES (1, 1, 'Bariloche');
INSERT INTO `destino` VALUES (2, 1, 'Buenos Aires');
INSERT INTO `destino` VALUES (3, 1, 'Calafate');
INSERT INTO `destino` VALUES (4, 1, 'Cordoba');
INSERT INTO `destino` VALUES (5, 1, 'Mar del Plata');
INSERT INTO `destino` VALUES (6, 1, 'Mensoza');
INSERT INTO `destino` VALUES (7, 1, 'Puerto de Iguazu');
INSERT INTO `destino` VALUES (8, 1, 'Puerto Madryn');
INSERT INTO `destino` VALUES (9, 1, 'Ushuaia');
INSERT INTO `destino` VALUES (10, 2, 'Aruba');
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `pais`
--
CREATE TABLE `pais` (
`id` int(11) NOT NULL auto_increment,
`paisname` varchar(255) NOT NULL,
`codigo3` char(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=22 ;
--
-- Volcar la base de datos para la tabla `pais`
--
INSERT INTO `pais` VALUES (1, 'Argentina', '0');
INSERT INTO `pais` VALUES (2, 'Aruba', '0');
INSERT INTO `pais` VALUES (3, 'Bahamas', '0');
INSERT INTO `pais` VALUES (4, 'Bolivia', '0');
INSERT INTO `pais` VALUES (5, 'Brasil', '0');
INSERT INTO `pais` VALUES (6, 'Chile', '0');
INSERT INTO `pais` VALUES (7, 'Colombia', '0');
INSERT INTO `pais` VALUES (8, 'Costa Rica', '0');
INSERT INTO `pais` VALUES (9, 'Cuba', '0');
INSERT INTO `pais` VALUES (10, 'Ecuador', '0');
INSERT INTO `pais` VALUES (11, 'Estados Unidos', '0');
INSERT INTO `pais` VALUES (12, 'Guatemala', '0');
INSERT INTO `pais` VALUES (13, 'Jamaica', '0');
INSERT INTO `pais` VALUES (14, 'Mexico', '0');
INSERT INTO `pais` VALUES (15, 'Nicaragua', '0');
INSERT INTO `pais` VALUES (16, 'Panama', '0');
INSERT INTO `pais` VALUES (17, 'Peru', '0');
INSERT INTO `pais` VALUES (18, 'Puerto rico', '0');
INSERT INTO `pais` VALUES (19, 'Republica Dominicana', '0');
INSERT INTO `pais` VALUES (20, 'Uruguay', '0');
INSERT INTO `pais` VALUES (21, 'Venezuela', '0');