doume1301 2018-05-16 11:40
浏览 165
已采纳

在PHP中使用会话变量提交表单后,如何在选择框中保留值

So I am building an example search page with PHP. It is a job search page with several web forms. When one of these is submitted I want to be able to save the values. I am trying to save them in a session variable and reload the select list, however I can't get it to work. Anyone know what I am doing wrong? Here is my code:

<!DOCTYPE html>
<?php
    session_start();

    if(isset($_POST['occupation']))
        $_SESSION['occupation'] = $_POST['occupation'];

    if(isset($_POST['work_type']))
        $_SESSION['work_type'] = $_POST['work_type'];

    if(isset($_POST['sort_by']))
        $_SESSION['sort_by'] = $_POST['sort_by'];

?>
<html>
<head>
    <style>
        table
        {
            width: 50%; margin: 0 auto;
        }
        .td-left
        {
            width: 20%;
        }
    </style>
</head>
<body>
<table>
    <tr>
        <td class="td-left" valign="top">
            <form method="post" action="">
                <h2>Keyword</h2>
                <input type="text" name="keyword"/>
                <h2>Industy</h2>
                <select name="occupation[]" multiple >
                    <?php 
                        $occ = array("Accounting", "Education", "Healthcare", "Information Technology", "Retail", "Sales");

                        $len = count($occ);

                        for($i = 0; $i<$len; $i++ )
                        {
                            if(in_array($occ, $_SESSION['occupation']))
                                echo '<option value="' . $i . '" selected>' . $occ[$i] .'</option>';
                            else
                                echo '<option value="' . $i . '" >' . $occ[$i] .'</option>';
                        } 
                    ?>
                </select>
                <br/><br/>
                <button type="submit" value="Search" class="my-button" name="search_button" >Search</button>
            </form>
        </td>
        <td style="float: right; " valign="top">
            <form method="post" action="">
                <h2>Sort By</h2>
                <select name="sort_by" onchange="this.form.submit()" >

                    <?php 
                        $sort_types = array("Date posted descending", "Date posted ascending", "Job title descending", "Job title ascending", "Location descending", "Location ascending", "Salary descending", "Salary ascending" );

                        $len = count($sort_types);

                        for($i = 0; $i<$len; $i++ )
                        {
                            if(in_array($sort_types, $_SESSION['sort_by'])) 
                                echo '<option value="' . $i . '" selected>' . $sort_types[$i] .'</option>';
                            else
                                echo '<option value="' . $i . '" >' . $sort_types[$i] .'</option>';
                        } 
                    ?>
                </select>
            </form>
        </td>
    </tr>
    <tr>
        <td class="td-left" valign="top">
            <form method="post" action="" name="refine_search">
                <h2>Refine Search</h2>
                <select name="work_type[]" multiple >
                    <?php 
                        $wt = array("Full Time", "Part Time", "Casual");

                        $len = count($wt);

                        for($i = 0; $i<$len; $i++ )
                        {
                            if(in_array($wt, $_SESSION['work_type']))
                                echo '<option value="' . $i . '" selected>' . $wt[$i] .'</option>';
                            else
                                echo '<option value="' . $i . '" >' . $wt[$i] .'</option>';
                        } 
                    ?>
                </select>
                <br/><br/>
                <button type="submit" value="Go" class="my-button" name="refine_search_button">Go</button>
            </form>
        </td>
        <td valign="top">
            <h2>Results</h2>
            Lorem Ipsum Dolor Sit Amet ...
        </td>
    </tr>
</table>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • douwen9345 2018-05-16 11:47
    关注

    Correct syntax for in_array is in_array(search,array,type) So :

    in_array($_SESSION['work_type'], $wt)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!