Skip to content
Advertisement

How to use $_SESSION variable to retrieve data and display it in another page [closed]

Below I have a php script where it displays a “Course” drop down menu and a “Module” Drop down menu. What is suppose to happen is the user first selects a course from the “Course” drop down menu and then a list of Modules which belongs to the selected course will appear in the “Modules” drop down menu. Below is the code for this:

create_session.php

$sql = "SELECT CourseId, CourseNo, CourseName FROM Course"; 
$sqlstmt=$mysqli->prepare($sql);
$sqlstmt->execute(); 
$sqlstmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName);
$courses = array(); // easier if you don't use generic names for data 

$courseHTML = "";  
$courseHTML .= '<select name="courses" id="coursesDrop">'.PHP_EOL; 
$courseHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

while($sqlstmt->fetch()) 
{ 
    $courseno = $dbCourseNo;
    $course = $dbCourseId;
    $coursename = $dbCourseName; 
    $courseHTML .= "<option value='".$course."'>" . $courseno . " - " . $coursename . "</option>".PHP_EOL;  
} 

$courseHTML .= '</select>'; 

?>

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <table>
        <tr>
            <th>Course: <?php echo $courseHTML; ?>
                <input id="courseSubmit" type="submit" value="Submit" name="submit" />
            </th>
        </tr>
    </table>
</form>

<?php

if (isset($_POST['submit'])) {

    $submittedCourseId = $_POST['courses'];

    $query = "SELECT cm.CourseId, cm.ModuleId, c.CourseNo, m.ModuleNo,
            c.CourseName,
            m.ModuleName
            FROM Course c
            INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
            JOIN Module m ON cm.ModuleId = m.ModuleId
            WHERE
            (c.CourseId = ?)
            ORDER BY c.CourseName, m.ModuleId";

    $qrystmt=$mysqli->prepare($query);

    // You only need to call bind_param once
    $qrystmt->bind_param("s",$submittedCourseId);
    // get result and assign variables (prefix with db)

    $qrystmt->execute(); 
    $qrystmt->bind_result($dbCourseId,$dbModuleId,$dbCourseNo,$dbModuleNo,$dbCourseName,$dbModuleName);

    $qrystmt->store_result();

    $num = $qrystmt->num_rows();

    if($num ==0){
        echo "<p style='color: red'>Please Select a Course</p>";
    } else { 

        $dataArray = array();

        while ( $qrystmt->fetch() ) { 
            // data array
            $dataArray[$dbCourseId]['CourseName'] = $dbCourseName; 
            $dataArray[$dbCourseId]['CourseNo'] = $dbCourseNo; 
            $dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleName'] = $dbModuleName; 
            $dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleNo'] = $dbModuleNo; 

            }

        foreach ($dataArray as $foundCourse => $courseData) {

            $output = ""; 

            $output .= "<p><strong>Course:</strong> " . $courseData['CourseNo'] .  " - "  . $courseData['CourseName'] . "</p>";

            $moduleHTML = ""; 
            $moduleHTML .= '<select name="module" id="modulesDrop">'.PHP_EOL;
            $moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;      
            foreach ($courseData['Modules'] as $moduleId => $moduleData) {        

                $moduleHTML .= "<option value='$moduleId'>" . $moduleData['ModuleNo'] . " - " . $moduleData['ModuleName'] ."</option>".PHP_EOL;        
            } 
        }
    $moduleHTML .= '</select>';

    echo $output;

?>

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <table>
        <tr>
            <th>Course: <?php echo $moduleHTML; ?>
                <input id="courseSubmit" type="submit" value="Submit" name="submit" />
            </th>
        </tr>
    </table>
</form>

So lets say I select the Course INFO101 - Information Communication Technology in the “Course” drop down menu, it displays the following modules in the Module drop down menu below which corresponds with that course:

CHI2520 - Advanced Web Programming
CHI2220 - Systems Strategy
CHI2350 - Interactive Systems

Now what I want to do is that I want the user to select one of the modules from the drop down menu and then submit the form below to navigate to the QandATable.php page. But I want to know how do I retrieve the module number and name from the drop down menu and display it in the QandATable.php page? I know I should use $_SESSION variable how and where do I write the $_SESSION variables in both pages? Also do I need to use isset so I don’t get an undefined variable?

Below is form which navigates to QandATable.php:

<form action="QandATable.php" method="post" id="sessionForm">
            <table><tr><th>6: Module:</th>
            <td><?php echo $moduleHTML; ?></td>
            </tr>
            </table>
            <p><strong>11: </strong><input class="questionBtn" type="submit" value="Prepare Questions" name="prequestion" /></p>   


        </form>

Advertisement

Answer

In answer to your questions. Yes, it is good practice to use an isset or is_null on the variable being posted initially to ensure you have a valid value. In the situation of writing it to a session variable though it would not matter. You can assign a null value to a session variable, but it could end up causing unintended results later if you try to use the session variable expecting a valid value.

To set a session variable you can do the following. You can set this anytime you need to in order to update the variable. I usually assign these on a post page somewhere after collecting the data from the user.

$_SESSION['variableName'] = $_POST['postedVariable'];

Remember that you also need the following code at the very top of every page that will use a session or could be hit with session variables established. If you do not have this code set on a page and the user visits it, you will lose your session tracking.

session_start();

To later reference a stored session variable you can do so like this:

echo $_SESSION['variableName'];

Hope I understood your question correctly.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement