0
votes

I have a basic mysqli code below where it performs a query to select CourseId and Course Name from database and display it in a drop down menu.

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

    $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" onchange="getModules();">'.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;  

    $outputcourse = ""; 
    $outputcourse .= "<p><strong>Course:</strong> " . $course .  " - "  . $coursename . "</p>";

    } 

    $courseHTML .= '</select>'; 

Now lets say the drop down menu contains these courses below:

INFO101 - Information Communication Technology
INFO102 - Computing

For some strange reason no matter which course I choose, it always echos the CourseId and CourseName INFO102 - Computing underneath the drop down menu. Why is this?

1
if the problem happens when you CHOOSE a course from the drop down menu maybe the problem is from javascript? - Naryl
@Naryl, please look at comment I sent to Abu below. The problem is when I select the option and them SUbmit. The problem isn't in the drop down menu. it is echoing what I have selected underneatch which is this code: $outputcourse = ""; $outputcourse .= "<p><strong>Course:</strong> " . $course . " - " . $coursename . "</p>"; - user1819709
it would help to see the code for getModules() function then. I mean, you have a problem when doing a submit using what you generate with the PHP above, but we have no code of the submit part.. so I can't really check what's wrong :S - Naryl
@Nayal I updated code to include code on course drop down menu, module drop down menu, the outputs of the course id and name and module id and name and getModules() function - user1819709

1 Answers

0
votes

<p><strong>Course:</strong>.....</p> This may be causing the problem,

because I don't think its valid to have tags other than <option></option> inside a <select></select> tag.

Try removing those stuff and see if the output appears in drop down.