我想从下拉菜单中按国家/地区过滤我的数据:
<form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="value">
<option name="country" value="AU">Austria</option>
<option name="country" value="BE">Belgium</option>
<option name="country" value="BU">Bulgaria</option>
</select>
<input type="submit" name="btn_submit" value="Submit Filter" />
<?php
if($_POST['country'] == 'BE') {
$query = mysql_query("SELECT * FROM think WHERE Country='Belgium'");
}
elseif($_POST['country'] == 'AU') {
$query = mysql_query("SELECT * FROM think WHERE Country='Austria'");
} else {
$query = mysql_query("SELECT * FROM think");
}
?>
代码不会过滤任何数据。 如果有人可以提供帮助,谢谢!
答案 0 :(得分:1)
当您使用select标签时,Server页面将引用select标签的名称而不是选项。
更改您的代码如下:
<select name="country">
<option value="AU">Austria</option>
<option value="BE">Belgium</option>
<option value="BU">Bulgaria</option>
</select>
答案 1 :(得分:0)
if($_POST['country'] == 'BE') {
应该是
if($_POST['value'] == 'BE') {
等等其他人!!
答案 2 :(得分:0)
<form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="country">
<option value="AU">Austria</option>
<option value="BE">Belgium</option>
<option value="BU">Bulgaria</option>
</select>
<input type="submit" name="btn_submit" value="Submit Filter" />
<?php
if($_POST['country'] == 'BE') {
$query = mysql_query("SELECT * FROM think WHERE Country='Belgium'");
}
elseif($_POST['country'] == 'AU') {
$query = mysql_query("SELECT * FROM think WHERE Country='Austria'");
} else {
$query = mysql_query("SELECT * FROM think");
}
?>
答案 3 :(得分:0)
避免编写冗余代码。 使用以下代码更改您的代码:
<form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="country">
<option value="AU">Austria</option>
<option value="BE">Belgium</option>
<option value="BU">Bulgaria</option>
</select>
<input type="submit" name="btn_submit" value="Submit Filter" />
<?php
if(isset($_POST['country']))
{
switch($_POST['country'])
{
case 'BE' : $countryName = 'Belgium';
break;
case 'AU' : $countryName = 'Austria';
break;
default : $countryName = '';
break;
}
$where = '';
if($countryName != '')
{
$where = "WHERE Country='".$countryName."'";
}
$query = mysql_query("SELECT * FROM think ".$where."");
}
?>