如何在PHP

时间:2015-07-08 09:22:45

标签: php

我的多选框创建如下:

<?php
    ini_set('display_errors',"1");

    $select = '<select multiple>';

    $lines = file('project-list.txt');
    $fifth_column = array();
    foreach ($lines as $line){
        $parts = preg_split('/\s+/', $line);
        $count = 0;
        foreach ($parts as $partVal){
            if ((in_array($partVal, $fifth_column) == FALSE) && $count == 4){
                $fifth_column[] = $partVal;
            }
            $count++;
        }
    }

    foreach($fifth_column as $value){


        $select .= "<option value='".$value."'>".$value."</option>";
    }

    $select .= '</select>';

    echo $select;





?>
<form action="" form method="post">
<input type="submit" class="ym-button ym-small" name = "Apply" style="margin-top:1em;max-width:11em" value="Apply" />
</form>

我希望能够在单击“应用”按钮时看到用户在框中选择了什么,我想使用此信息在project-list.txt中搜索他们选择的值,然后显示包含该值的行。我一直在尝试使用isset($ _ GET)但是我的选择框没有名称。

增加:

例如:

如果我的列表框包含: ITEM1 ITEM2 项目3 ITEM4

并且用户选择item1,我想在item1的text-list.txt文本文件中搜索并打印该行。所以我的文本文件可能如下所示:

xxxx  hallone item 1
xxxx  halltwo item 2
xxxx  hallthree item 3
xxxx  hallfour item 1

点击“应用”按钮后,选择item1后,应该发布结果:

xxxx  hallone item 1
xxxx  hallfour item 1

1 个答案:

答案 0 :(得分:1)

试用此代码

    <form action="" method="post"> 
    <?php
        ini_set('display_errors',"1");

        $select = '<select multiple name = "project[]">'; // give name attribute to your select box

        $lines = file('project-list.txt');
        $fifth_column = array();
        foreach ($lines as $line){
            $parts = preg_split('/\s+/', $line);
            $count = 0;
            foreach ($parts as $partVal){
                if ((in_array($partVal, $fifth_column) == FALSE) && $count == 4){
                    $fifth_column[] = $partVal;
                }
                $count++;
            }
        }

        foreach($fifth_column as $value){


            $select .= "<option value='".$value."'>".$value."</option>";
        }

        $select .= '</select>';

        echo $select;





    ?>
    <input type="submit" class="ym-button ym-small" name = "Apply" style="margin-top:1em;max-width:11em" value="Apply" />
    </form>
<?php
if(isset($_POST) && !empty($_POST['project'])){
    print_r($_POST['project']);
}
?>