为 Make 执行下面给出的 html
<?php $sql = "SELECT * FROM country";
$result = mysql_query($sql);
?>
<select id="country" name='country' onchange="get_states();">
<option value=''>Select</option>
<?php while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['country_id'] . "'>" . $row['country_name'] . "</option>";}
?>
</select>
<div id="get_state"></div> // Sub will be appended here using ajax
写一个ajax函数get_states();
<script type="text/javascript">
function get_states() { // Call to ajax function
var country = $('#country').val();
var dataString = "country="+country;
$.ajax({
type: "POST",
url: "getstates.php", // Name of the php files
data: dataString,
success: function(html)
{
$("#get_state").html(html);
}
});
}
</script>
文件 getstates.php - 将从下面的文件中获取子文件,该文件将附加到 div
if ($_POST) {
$country = $_POST['country'];
if ($country != '') {
$sql1 = "SELECT * FROM state WHERE country=" . $country;
$result1 = mysql_query($sql1);
echo "<select name='state'>";
echo "<option value=''>Select</option>";
while ($row = mysql_fetch_array($result1)) {
echo "<option value='" . $row['state_id'] . "'>" . $row['state_name'] . "</option>";}
echo "</select>";
}
else
{
echo '';
}
}