0

I have something strange going on with my application. Below is my code where the user types in a courseid into a text input and submits the text input. If it finds the course from the DB then it displays the echo that course is found, else it states Course isn't found. Now this works across all browsers (IE, Opera, Safari, Firefox and Chrome). Below is the code for this:

<h1>CREATING A NEW ASSESSMENT</h1>

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Course ID: <input type="text" name="courseid" /><input id="courseSubmit" type="submit" value="Submit" name="submit" /></p>      <!-- Enter User Id here-->
</form>

<?php
if (isset($_POST['submit'])) {

$query = "
SELECT cm.CourseId, cm.ModuleId, 
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",$courseid);
// get result and assign variables (prefix with db)

$qrystmt->execute(); 

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

$qrystmt->store_result();

$num = $qrystmt->num_rows();

if($num ==0){
echo "<p>Sorry, No Course was found with this Course ID '$courseid'</p>";
} else { 

echo "<p>Course Found: '$courseid'</p>";

}

But now I have decided to change the set up so instead of typing in the courseID in a text input, they can select a course ID from a drop down menu. So I change the code to this below:

$sql = "SELECT CourseId, CourseName FROM Course"; 

$sqlstmt=$mysqli->prepare($sql);

$sqlstmt->execute(); 

$sqlstmt->bind_result($dbCourseId, $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()) 
{ 
$course = $dbCourseId;
$coursename = $dbCourseName; 
$courseHTML .= "<option value='".$course."'>" . $course . " - " . $coursename . "</option>".PHP_EOL;  
} 

$courseHTML .= '</select>'; 
$courseHTML .= '</form>'; 

?>

<h1>CREATING A NEW ASSESSMENT</h1>

<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.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,$dbCourseName,$dbModuleName);

$qrystmt->store_result();

$num = $qrystmt->num_rows();

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

echo "<p style='color: green'>Course Found '$courseid'</p>";

}

But this is the strange thing, the drop down menu works in Chrome, Firefox and Safari but not in Opera and IE. My question then is that is there something in the second block of code which is stopping it from working in IE or Opera?

1
  • You are outputting two </form> tags, one before the submit button. Remove this $courseHTML .= '</form>';
    – air4x
    Oct 27, 2012 at 13:45

1 Answer 1

0

Remove $courseHTML .= '</form>';, It'll work.

The fact is when you are adding </form> to $coureseHTML and adding this in side a form. The form closing tag finishes the form. And the rest of the code does not belong to the form. Here, <input id="courseSubmit" type="submit" value="Submit" name="submit" /> does not belong to preceding form. So clicking this button wont work. It should not work in any browser. May be its working on chrome, firefox and safari by luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.