Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8901053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:15:14+00:00 2026-06-15T01:15:14+00:00

Below I have a php script where it displays a Course drop down menu

  • 0

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; 

            // session data
            $_SESSION['idcourse'] = $dbCourseNo;
            $_SESSION['namecourse'] = $dbCourseName;
            $_SESSION['idmodule'] = $dbModuleNo;
            $_SESSION['namemodule'] = $dbModuleName;

            }

        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 this is the problem I have. If I select the module CHI2520 - Advanced Web Programming and then access the page below, it displays this module instead CHI2350 - Interactive Systems.

QandATable.php:

    <?php

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

        $_SESSION['idmodule'] = $_POST['idmodule'];

    }

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

        $_SESSION['namemodule'] = $_POST['namemodule'];

    }

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

$_SESSION['idcourse'] = $_POST['idcourse'];

}

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

$_SESSION['namecourse'] = $_POST['namecourse'];

}


    $outputDetails = "";
    $outputDetails .= "
    <table id='sessionDetails' border='1'>
    <tr>
    <th>Course:</th> 
    <th>{$_SESSION['idcourse']}  {$_SESSION['namecourse']}</th>
    </tr>
<tr>
<th>Module:</th> 
<th>{$_SESSION['idmodule']}  {$_SESSION['namemodule']}</th>
</tr>
    </table>
    ";

    echo $outputDetails;

    ?>

My question is that why does it display the wrong module number and name in the other page? Both page does include session_start();

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:15:16+00:00Added an answer on June 15, 2026 at 1:15 am

    You cannot assign php sessions once the page is loaded already! Well you could but not just with php itself! let me explain:

    • first request ( the form gets downloaded – no modules displayed )

    • second request ( new form gets downloaded – based on the courseid which has been submitted you display the right modules )

    • third request ( based on the moduleid submitted you have to populate php sessions for later use )

    What you were trying to do is to populate the session vars during the second request… however, there’s no way you could ever retrieve the right moduleid by doing so! Only the browser and eventually javascript know what’s selected at that specific point in time when the page is living just inside the browser!

    What you actually did, as i just said, is to write your sessions in the second request. That means that you wrote your session vars prior to the user’s module selection! And what you put to session was nothing more than the last fetched result row in the loop! That is the reason why you always had the last module in your session, which in your case was Interactive Systems!

    Let’s find a solution now

    Provided your course / module selector works properly, displaying the right modules once the users selects its course, you just have to make your third request!

    <form name="moduleid" [...] >
    
        <option value="[moduleid]> [moduleno] - [modulename] </option>
        
        [...]
        
    </form>
    

    Based on a structure like this… i just guess this is what your module form could look like… you have to perform the third request to the server which can be either on the same page, as you did for adding the modules select, or on an another one! the clue code here is something like the one below… you don’t even have to generate any output 😉

    if ( isset($_POST['moduleid']) )
    {
        $stmt = $mysqli->prepare('select moduleno, modulename from  module where moduleid = ?');
        
        $stmt->bind_param('s', $_POST['moduleid'] );
        
        if ( $stmt->execute() )
        {   
                $stmt->bind_result($moduleno, $modulename); $stmt->fetch();
    
            $_SESSION['moduleid'] = $_POST['moduleid'];
            $_SESSION['moduleno'] = $moduleno;
            $_SESSION['modulename'] = $modulename;
        }
    }
    

    if your course / module selector does not work properly… reffer to my other answer 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below I have a php script where it displays a Course drop down menu
I have php/mysqli and jquery code below where it displays a course drop down
I have this code below in the module.php script where it displays options in
I have a php script that displays a web form to the user. When
I have a small script which im using to test PHP mail(), as below:
I have a php script below known as cancelimage.php where it will display a
I have a php script below known as cancelimage.php where it will display a
I have a directory that I've built with the PHP script below and it
I have this javascript and php/html code below: <script> var examInput = document.getElementById('newAssessment').value; var
I have a PHP script which displays a PDF inside an object element. Adobe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.