我有一张名为餐馆的桌子,里面有餐馆的详细信息,如名称、地址等。
我希望创建一个评论部分,允许用户通过每个餐厅的表格进行评分和评论。目前,我的表单中有两个下拉菜单,一个是County,另一个是RestaurantName,我希望用户选择一个County,然后在下一个下拉菜单中只填充该County的餐厅。
最好的方法是什么?
如果你需要更多信息,请告诉我。我目前在两个下拉列表中都填充了mySql表中的数据,但它显示了当前所有的餐厅。
drop.php
<?php
mysql_connect("localhost", "B00606958", "uHmB4jRw") or die("Error connecting to database: ".mysql_error());
/*
localhost - location of mysql server,
if connection fails it will stop loading the page and display an error
*/
mysql_select_db("b00606958") or die(mysql_error());
/* tutorial_search is the name of database we've created */
?>
<?php
$query_parent = mysql_query("SELECT DISTINCT County FROM restaurants") or die("Query failed: ".mysql_error());
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dependent DropDown List</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#parent_cat").change(function() {
$(this).after('<div id="loader"><img src="images/loading.gif" alt="loading subcategory" /></div>');
$.get('loadsubcat.php?parent_cat=' + $(this).val(), function(data) {
$("#sub_cat").html(data);
$('#loader').slideUp(200, function() {
$(this).remove();
});
});
});
});
</script>
</head>
<body>
<form method="get">
<label for="category">County</label>
<select name="parent_cat" id="parent_cat">
<?php while($row = mysql_fetch_array($query_parent)): ?>
<option value="<?php echo $row['County']; ?>"><?php echo $row['County']; ?></option>
<?php endwhile; ?>
</select>
<br/><br/>
<label>Restaurant</label>
<select name="sub_cat" id="sub_cat"></select>
</form>
</body>
</html>
loadsubcat.php
mysql_connect("localhost", "B00606958", "uHmB4jRw") or die("Error connecting to database: ".mysql_error());
/*
localhost - location of mysql server,
if connection fails it will stop loading the page and display an error
*/
mysql_select_db("b00606958") or die(mysql_error());
/* tutorial_search is the name of database we've created */
$parent_cat = $_GET['parent_cat'];
$query = mysql_query("SELECT RestaurantID FROM restaurants WHERE County = {$parent_cat}");
while($row = mysql_fetch_array($query)) {
echo "<option value='$row[RestaurantID]'>$row[RestaurantName]</option>";
}
?>
表格结构:
Tbl name=餐馆
1餐厅名称varchar(255)
2地址行1 varchar(100)
3地址行2 varchar(100)
4 Town varchar(50)
5县varchar(50)
6邮政编码varchar(7)
7电话varchar(11)
8电子邮件varchar(50)
9网站varchar(100)
10道菜int(255)
11餐厅ID bigint(20)
12评级int(5)
有两种方法:
- 当用户更改Country选择值时,发出Ajax请求
- 在餐厅选项中注册对国家的引用;当用户更改国家/地区时,隐藏与所选国家/地区参考不匹配的每个餐厅选择(实现取决于您使用的语言)
1)不要在每一页中都写connection code
。写在一页&包括在需要的每一页中。我创建了一个页面,即dbConnection.php页面。
使用此代码。而且,如果出现任何错误。请随意提问。
dbConnection.php
<?php
$con = mysql_connect("localhost", "B00606958", "uHmB4jRw") or die("Error connecting to database: ".mysql_error());
mysql_select_db("b00606958",$con) or die(mysql_error());
?>
drop.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dependent DropDown List</title>
</head>
<body>
<?php include('dbConnection.php');?> //connection here.
<form method="get">
<label for="category">County</label>
<select name="parent_cat" id="parent_cat">
<?php
$query_parent = mysql_query("SELECT DISTINCT County FROM restaurants") or die("Query failed: ".mysql_error());
while($row = mysql_fetch_array($query_parent)): ?>
<option value="<?php echo $row['County']; ?>"><?php echo $row['County']; ?></option>
<?php endwhile; ?>
</select>
<br/><br/>
<label>Restaurant</label>
<div class="RestaurantDiv"></div>
</form>
</body>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#parent_cat").change(function() {
$(this).after('<div id="loader"><img src="images/loading.gif" alt="loading subcategory" /></div>');
var county = $(this).val();
$.ajax({url:"loadsubcat.php?parent_cat="+county,cache:false,success:function(result){
$(".RestaurantDiv").html(data);
$('#loader').slideUp(200, function() {
$(this).remove();
});
}});
});
});
</script>
</html>
loadsubcat.php
<?php
include('dbConnection.php');
$parent_cat = $_GET['parent_cat'];
$query = mysql_query("SELECT * FROM restaurants WHERE County = {$parent_cat}");
?>
<select name="sub_cat" id="sub_cat"></select>
<?
while($row = mysql_fetch_array($query)) {?>
<option value="<?echo $row['RestaurantID'];?>"><?php echo $row['RestaurantName']?></option>
<?}?>
</select>