地区表
id | region
-------------------
1 | Region1
2 | Region2
…
和学校的桌子
id | school
-------------------
1 | schno1
1 | schno5
1 | schno6
2 | scho120
<select name="region">
<option value="0">Select the region</option>
<?PHP
$result=$db->query("SELECT * FROM regions");
while($row=$result->fetch_array(MysqLI_BOTH))
{
echo '<option value="'.$row[0].'">'.$row[1].'</option>';
}
?>
</select>
我想要做的是,获取“地区”ID,然后在“学校”表中即时填写基于id(之前选择的ID)的学校下拉菜单.我是js的新手.请帮我修理一下. Thx提前.
解决方法:
$region = MysqL_real_escape_string($_POST['region']);
$query = "SELECT s.school FROM regions r
INNER JOIN schools s ON (s.region_id = r.id)
WHERE r.region LIKE '$region' "; <<-- LIKE is case insensitive, '=' is NOT
$result = $db->query($query);
if not($result) then { die("error"); }
while($row=$result->fetch_array(MysqLI_BOTH))
{
echo '<option value="'.$row[0].'">'.$row[1].'</option>';
}