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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改