ICT Teacher
ICT Teacher

Reputation: 37

php select drop down choice to session variable

I have two dropdown boxes and I want the user to choose values from both. The dropdown boxes are filled with results of a query on page load. I want these values to then be stored as a session variable for use in subsequent queries.

First file = choose.php

<html>
    <form action="choose2.php" method="post">
    <?//db connection stuff taken out from here
        }
// fill classes      
    $result = mysqli_query($con,"SELECT * FROM classes");                                            
    echo "<select id='classlist'>";

    while($row = mysqli_fetch_array($result))
        {
        echo " <option value=" . $row['id'] . ">" . $row['classname'] . "</option>";
        }
    echo "</select>";

    //fill schemes of work
    $result = mysqli_query($con,"SELECT * FROM schemes_of_work");
    echo "<select id='sowlist'>";

    while($row = mysqli_fetch_array($result))
        {
        echo " <option value=" . $row['id'] . ">" . $row['name'] . "</option>";
        }
    echo "</select>";

    mysqli_close($con);
    ?>   
<input type="submit">
</form> 
</html>

second file = choose2.php

 <?php  session_start(); 
    $_SESSION['classname'] = $_POST["classlist"];
    $_SESSION['sowname'] = $_POST["sowlist"];
    ?>

    <?php echo $_SESSION['classname'];?>
    <?php echo $_SESSION['sowname'];?>

I cant get this to work though - I am getting an empty page on choose2 and the following error in apache log: "Undefined index: sowlist in /var/www/assessment/choose2.php on line 3,

Upvotes: 0

Views: 1405

Answers (1)

d4rkpr1nc3
d4rkpr1nc3

Reputation: 1827

The select tag should have a name attribute to be able to use it in php POST. Try

<select id='classlist' name="classlist">

The same for the other one.

Upvotes: 2

Related Questions