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

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,