从表单中的多个选择标记查询

时间:2014-03-21 13:58:42

标签: php sql

我在表单中有三个选择标记:

   <form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="country">
    <option name="country" value="AL">All</option>
    <option name="country" value="AU">Austria</option>
    <option name="country" value="BE">Belgium</option>

</select>
Select Type:
<select name="ASS">
    <option name="ASS" value="all">All</option>
    <option name="ASS" value="think">Think Tanks</option>
    <option name="ASS" value="network">Networks</option>
    <option name="ASS" value="NGO">NGOs</option>
</select>
Select EU Attitude:
<select name="EU">
    <option name="EU" value="all">All</option>
    <option name="EU" value="PRO">PRO EU</option>

</select>
<input type="submit" name="btn_submit" value="Submit Filter" />
</form>

我为每个选择标记制作了三个这样的函数,但是当我过滤结果时,它只考虑最后一个选择标记:

<?php
if(isset($_POST['country']))
{
    switch($_POST['country'])
    {
    case 'BE' : $countryName = 'Belgium';
                break;
    case 'AU' : $countryName = 'Austria';
                break;
    case 'BU' : $countryName = 'Bulgaria';
                break;

    default : $countryName = '';
              break;
    }
    $where = '';
    if($countryName != '')
    {
        $where = "WHERE Country='".$countryName."'";
    }   
}

if(isset($_POST['ASS']))
{
    switch($_POST['ASS'])
    {
    case 'think' : $type = 'Think Tank';
                break;
    case 'network' : $type = 'Network';
                break;
    case 'NGO' : $type = 'NGO';
                break;
    default : $type = '';
              break;
    }
    $where = '';
    if($type != '')
    {
         if(!empty($type)){
        $where .= " AND Type='".$type."'";
    } else {
        $where = "WHERE Type='".$type."'";
    }   
    }   

}


if(isset($_POST['EU']))
{
    switch($_POST['EU'])
    {
    case 'PRO' : $eus = 'PRO EU';
                break;
    case 'REFORM' : $eus = 'Reform EU';
                break;
    case 'ANTI' : $eus = 'Eurosceptic';
                break;
    default : $eus = '';
              break;

    }
    $where = '';
    if($eus != '')
    {
        if((!empty($countryName)) || (!empty($type))){
        $where .= " AND Attitude='".$eus."'";
    } else {
        $where = "WHERE Attitude='".$eus."'";
        }
        }
        $query = mysql_query("SELECT * FROM think ".$where.""); 
}
?>

单击提交按钮时,我想从表单中查询所有选定的选项,但这不会发生。它只考虑最后一个选项。 谢谢!

2 个答案:

答案 0 :(得分:0)

您应该只提供<select>名称。选项只需要价值。你也没有给你的<form>方法,如果我没弄错,默认是GET。

<form method='post'>
<select name="ASS">
    <option value="all">All</option>
    <option value="think">Think Tanks</option>
</select>
<select name="EU">
    <option value="all">All</option>
    <option value="PRO">PRO</option>
</select>
<select name="EU2">
    <option value="all">E</option>
    <option value="PRO">A</option>
</select>
<input type="submit"/>
</form>

然后你需要做的就是获得所选的值:

$selected_value_ASS = $_POST['ASS'];

你也检查过&#34; NGO&#34;在第一个选择中,此值没有任何选项。您也使用其他<select>标记。

答案 1 :(得分:0)

这是一个完整的代码更新,因为我现在拥有完整的代码。我删除了名称=&#34;&#34;来自您选择的每个选项。只要select标签有名称,您就不需要为每个选项命名。

在您发布的修订代码中,您还在每个块中重置$ where的值 - 但我们需要保留$ where的值,并可能在每个块中添加它。如果用户选择所有三个选项,我们需要不断构建$ where变量。如果它在每个块中重置,那么您将始终仅获取所选的最后一个选项。

我还通过更改选项中的选项值来完全删除case语句,以反映您想要的最终值。我只需要添加一个if语句来捕获他们是否已选择&#34; all&#34;。我做了所有&#34; all&#34;选项也是统一的。

<form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="country">
    <option value="all">All</option>
    <option value="Austria">Austria</option>
    <option value="Belgium">Belgium</option>
</select>
Select Type:
<select name="ASS">
    <option value="all">All</option>
    <option value="Think Tank">Think Tanks</option>
    <option value="Network">Networks</option>
    <option value="NGO">NGOs</option>
</select>
Select EU Attitude:
<select name="EU">
    <option value="all">All</option>
    <option value="PRO EU">PRO EU</option>
    <option value="Reform EU">Reform EU</option>
    <option value="Eurosceptic">Eurosceptic</option>
</select>
<input type="submit" name="btn_submit" value="Submit Filter" />
</form>
</body>
</html>

<?php

if(isset($_POST['country'])){
    $countryName = $_POST['country'];
    if ($countryName == "all") {
        $countryName = '';
    }   
    if($countryName != ''){
        $where = "WHERE Country='".$countryName."'";
    }   
}

if(isset($_POST['ASS'])){
    $type = $_POST['ASS'];
    if ($type == "all") {
        $type = '';
    }
    if($type != ''){
         if((!empty($countryName)) && ($type != '')){
            $where .= " AND Type='".$type."'";
         } else if ($type != '') {
            $where = "WHERE Type='".$type."'";
        }   
    }   
}

if(isset($_POST['EU'])){
    $eus = $_POST['EU'];
    if ($eus == "all") {
        $eus = '';
    }
    if($eus != ''){
        if(((!empty($countryName)) || (!empty($type))) && ($eus != '')){
            $where .= " AND Attitude='".$eus."'";
        } else if ($eus != '') {
            $where = "WHERE Attitude='".$eus."'";
        }
    }
}

if ($where) {
    $query = mysql_query("SELECT * FROM think ".$where.""); 
} else {
    echo "No choices selected.";
}


?>