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>
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!
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 your course / module selector does not work properly… reffer to my other answer 😉