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

How do I keep value in select box after form submit using a session variable in PHP

所以我正在使用 PHP 构建示例搜索页面。这是一个包含多个 Web 表单的求职页面。当其中一个被提交时,我希望能够保存这些值。我试图将它们保存在会话变量中并重新加载 select 列表,但是我无法让它工作。有人知道我做错了什么吗?这是我的代码:

<!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>

in_array 的正确语法是 in_array(search,array,type) 所以:

in_array($_SESSION['work_type'], $wt)

检查一个值或字符串是否在数组中的方法如下:

if (in_array(needle, haystack)) {
        echo "needle";
    }

你的情况:

for($i = 0; $i<$len; $i++ ){
      if(in_array($_SESSION['occupation'],$occ ))
      echo "$_SESSION['occupation']";
      }else
      echo 'something'; }