Tengo una inquietud y es como hacer una lista dependiente, pero solo es consultar una tabla,me explico:
Tengo una tabla que se llama destinos, donde tiene como campos Id, Ciudad y Sigla.
Ejemplo:
Id = 1
Ciudad = Madrid
Sigla = MD
y en el form La idea es que al dar clic en la lista desplegable donde me muestra el nombre de la ciudad, en la otra lista desplegable cambie el valor de la sigla automaticamente. Agradezco mucho sus ayudas.
Código PHP:
<?php require_once('../../../Connections/conex.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$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;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO usuarios (Nombres, Apellidos, Identificacion, Movil, email, Usuario, Password, Estatus, CiudaTrabajo) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['Nombres'], "text"),
GetSQLValueString($_POST['Apellidos'], "text"),
GetSQLValueString($_POST['Identificacion'], "text"),
GetSQLValueString($_POST['Movil'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['Usuario'], "text"),
GetSQLValueString($_POST['Password'], "text"),
GetSQLValueString($_POST['Estatus'], "int"),
GetSQLValueString($_POST['CiudaTrabajo'], "text"));
mysql_select_db($database_conex, $conex);
$Result1 = mysql_query($insertSQL, $conex) or die(mysql_error());
$insertGoTo = "Creado.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
mysql_select_db($database_conex, $conex);
$query_Recordset1 = "SELECT * FROM usuarios";
$Recordset1 = mysql_query($query_Recordset1, $conex) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
mysql_select_db($database_conex, $conex);
$query_Ciudades = "SELECT Ciudad, Sigla FROM destinos";
$Ciudades = mysql_query($query_Ciudades, $conex) or die(mysql_error());
$row_Ciudades = mysql_fetch_assoc($Ciudades);
$totalRows_Ciudades = mysql_num_rows($Ciudades);
?>
<!DOCTYPE html >
<html>
<head>
<title>Petrominerales Control de Correspondecia</title>
<meta charset="iso-8859-1" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<!--[if IE 6]>
<link href="css/ie6.css" rel="stylesheet" type="text/css" />
<![endif]-->
<!--[if IE 7]>
<link href="css/ie7.css" rel="stylesheet" type="text/css" />
<![endif]-->
<link href="../../css/ie6.css" rel="stylesheet" type="text/css">
<link href="../../css/ie7.css" rel="stylesheet" type="text/css">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
<link href="../css/style.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.Estilo1 { color: #000000;
font-weight: bold;
}
.Estilo5 {color: #0000FF}
.Estilo6 {color: #000000}
-->
</style>
</head>
<body>
<div id="background">
<div id="page">
<div class="header">
<div class="footer">
<div class="body">
<div id="content">
<div class="content">
<ul class="article"><li>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td nowrap align="right"><strong>Ciuda de Trabajo:</strong></td>
<td><label>
<select name="CiudaTrabajo" id="CiudaTrabajo">
<?php
do {
?>
<option value="<?php echo $row_Ciudades['Ciudad']?>"><?php echo $row_Ciudades['Ciudad']?></option>
<?php
} while ($row_Ciudades = mysql_fetch_assoc($Ciudades));
$rows = mysql_num_rows($Ciudades);
if($rows > 0) {
mysql_data_seek($Ciudades, 0);
$row_Ciudades = mysql_fetch_assoc($Ciudades);
}
?>
</select>
<select name="Sigla" id="Sigla">
<?php
do {
?>
<option value="<?php echo $row_Ciudades['Sigla']?>"><?php echo $row_Ciudades['Sigla']?></option>
<?php
} while ($row_Ciudades = mysql_fetch_assoc($Ciudades));
$rows = mysql_num_rows($Ciudades);
if($rows > 0) {
mysql_data_seek($Ciudades, 0);
$row_Ciudades = mysql_fetch_assoc($Ciudades);
}
?>
</select>
</label></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td><input type="submit" value="Insertar registro"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
<p> </p>
</li>
<li><strong></strong></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="shadow">
</div>
</div>
</div>
</body>
</html>
<?php
mysql_free_result($Recordset1);
mysql_free_result($Ciudades);
?>