Search code examples
phphtmlsession-variablesform-submitpage-refresh

How do I keep value in select box after form submit using a session variable in 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>

Solution

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

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