Código PHP:
<?php require_once('../Connections/dboptchile.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_dboptchile, $dboptchile);
$query_regiones = "SELECT * FROM regiones ORDER BY REGION_ID ASC";
$regiones = mysql_query($query_regiones, $dboptchile) or die(mysql_error());
$row_regiones = mysql_fetch_assoc($regiones);
$totalRows_regiones = mysql_num_rows($regiones);
$colname_comunas = "-1";
if (isset($_POST['REGION_ID'])) {
$colname_comunas = $_POST['REGION_ID'];
}
mysql_select_db($database_dboptchile, $dboptchile);
$query_comunas = sprintf("SELECT COMUNA_ID, COMUNA_NOMBRE FROM comunas WHERE REGION_ID = %s ORDER BY COMUNA_ID ASC", GetSQLValueString($colname_comunas, "int"));
$comunas = mysql_query($query_comunas, $dboptchile) or die(mysql_error());
$row_comunas = mysql_fetch_assoc($comunas);
$totalRows_comunas = mysql_num_rows($comunas);
?>
<!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>
</head>
<body>
<form method="post" name="prueba" id="prueba" lang="es" onchange='submit();'>
<p>
<label for="region_id"></label>
<select name="region_id" id="region_id">
<option value="" <?php if (!(strcmp("", $_POST['region_id']))) {echo "selected=\"selected\"";} ?>>Selecciona una Región</option>
<?php
do {
?>
<option value="<?php echo $row_regiones['REGION_ID']?>"<?php if (!(strcmp($row_regiones['REGION_ID'], $_POST['region_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_regiones['REGION_NOMBRE']?></option>
<?php
} while ($row_regiones = mysql_fetch_assoc($regiones));
$rows = mysql_num_rows($regiones);
if($rows > 0) {
mysql_data_seek($regiones, 0);
$row_regiones = mysql_fetch_assoc($regiones);
}
?>
</select>
</p>
<p>
<label for="comuna_id"></label>
<select name="comuna_id" id="comuna_id">
<option value="" <?php if (!(strcmp("", $_POST['comuna_id']))) {echo "selected=\"selected\"";} ?>>Selecciona una Comuna</option>
<?php
do {
?>
<option value="<?php echo $row_comunas['COMUNA_ID']?>"<?php if (!(strcmp($row_comunas['COMUNA_ID'], $_POST['comuna_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_comunas['COMUNA_NOMBRE']?></option>
<?php
} while ($row_comunas = mysql_fetch_assoc($comunas));
$rows = mysql_num_rows($comunas);
if($rows > 0) {
mysql_data_seek($comunas, 0);
$row_comunas = mysql_fetch_assoc($comunas);
}
?>
</select>
</p>
</form>
</body>
</html>
<?php
mysql_free_result($regiones);
mysql_free_result($comunas);
?>