我正在尝试使一种形式从数据库中触发另一种形式的填充。我已经对 HTML、PHP 和数据库进行了排序,但我正在努力使用 jQuery。
有人能把我引向正确的方向吗?我已经用谷歌搜索死了,但我有点受限于我不知道要搜索什么的事实。
编辑:
这是我听从你的建议的。不幸的是,它仍然无法正常工作。
jQuery:
<script type="text/javascript">
$('#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="'+city[0]+'">'+city[1]+'</option>');
});
});
});
</script>
HTML:
<form name="myform" action="" method="POST">
<h1>1. Choose your city</h1>
<select name="city" class="dropdown" id="city">
<option value="default" disabled="disabled" selected="selected">--- Select your option ---</option>
<?php getCities(); ?>
</select>
<h1>2. Choose your club</h1>
<select name="club" class="dropdown" id="club">
<option value="default" disabled="disabled" selected="selected">--- Select your option ---</option>
</select>
</form>
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);
?>