我想从下拉菜单中按国家筛选数据:
<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");
}
?>代码不过滤任何数据。如果有人能帮忙的话,谢谢!
发布于 2014-03-21 10:50:31
避免编写多余的代码。用以下代码更改您的代码:
<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."");
}
?>发布于 2014-03-21 10:41:36
使用select标记时,服务器页将引用select标记的名称,而不是选项。
更改代码如下:
<select name="country">
<option value="AU">Austria</option>
<option value="BE">Belgium</option>
<option value="BU">Bulgaria</option>
</select>发布于 2014-03-21 10:38:14
if($_POST['country'] == 'BE') { 应该是
if($_POST['value'] == 'BE') { 等等等等!
https://stackoverflow.com/questions/22556394
复制相似问题