我已经在这里得到了一些帮助,但不幸的是,我仍在为级联下拉菜单而苦苦挣扎。我需要根据上面的一个框<select>
的选择来更新下面的内容。#city
<select>
我的代码中是否有任何明显的错误?
HTML/jQuery:
<select name="club" class="dropdown" id="club">
<script>
$('#city').change(function(){
var $club = $('#club');
$club.find('option:not([value="default"])').remove(); //Remove previous items
$.getJSON('getClubs.php', {city:$(this).val()}, function(clubs){
$.each(clubs, function(index, city){
$club.append('<option value="'+row[0]+'">'+row[1]+'</option>');
});
});
});
</script>
</select>
PHP (getClubs.php):
<?php
date_default_timezone_set('Europe/London');
$day = date("l");
$time = date("G");
if ($time >= 21) {
$day = date('l', strtotime($day .' +1 day'));
}
$city = $_POST['city'];
if ($day == Monday) {
$query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY')";
}
else if ($day == Tuesday) {
$query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY')";
}
else if ($day == Wednesday) {
$query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY')";
}
else if ($day == Thursday) {
$query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY')";
}
else if ($day == Friday) {
$query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY')";
}
else if ($day == Saturday) {
$query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY')";
}
else if ($day == Sunday) {
$query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY')";
}
$result = mysql_query($query);
$items = array();
if($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$items[] = array( $row[0], $row[1] );
}
}
mysql_close();
// convert into JSON format and print
echo json_encode($items);
?>