I have a drop down menu below and underneath that some text inputs. What I am trying to do is that when I select a course from the drop down menu, it should display the relevant details of the course selected in the text inputs. But nothing is being displayed in the text inputs. Now I am receiving no errors but I believe I am doing something incorrectly in my code for course details not being displayed in text inputs. How can I get the details displayed in the text inputs?
Below is mysqli code of drop down menu and retrieving Duration for each course:
$coursequery = "
SELECT CourseId, CourseNo, CourseName, Duration, CourseActive
FROM Course
WHERE
(CourseActive = ?)
ORDER BY CourseNo
";
... //mysqli prepare
$courseqrystmt->execute();
$courseqrystmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName, $dbCourseDuration, $dbCourseActive);
$courseqrystmt->store_result();
$coursenum = $courseqrystmt->num_rows();
$courseHTML = '';
$courseHTML = '<select name="course" id="coursesDrop">'.PHP_EOL;
$courseHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$studentInfo = array();
$courseInfo = array();
while ( $courseqrystmt->fetch() ) {
$courseHTML .= sprintf("<option value='%s'>%s - %s</option>", $dbCourseId, $dbCourseNo, $dbCourseName) . PHP_EOL;
$courseData = array();
$courseData["Duration"] = $dbCourseDuration;
array_push($courseInfo, $courseData);
}
$courseHTML .= '</select>';
Below is code for text inputs:
$editsession = "
<form id='updateForm'>
<p><strong>Course Chosen:</strong></p>
<table>
<tr>
<th></th>
<td><input type='hidden' id='currentId' name='Idcurrent' readonly='readonly' value='' /> </td>
</tr>
<tr>
<th>Course No:</th>
<td><input type='text' id='currentCourseNo' name='CourseNocurrent' readonly='readonly' value='' /> </td>
</tr>
<tr>
<th>Course Name:</th>
<td><input type='text' id='currentCourseName' name='CourseNamecurrent' readonly='readonly' value='' /> </td>
</tr>
<tr>
<th>Duration:</th>
<td><input type='text' id='currentDuration' name='Durationcurrent' readonly='readonly' value='' /> </td>
</tr>
</table>
</form>
";
echo $editsession;
Below is jquery code where it displays data in text inputs depending on course selected from course drop down menu:
$(document).ready( function(){
var courseinfo = <?php echo json_encode($courseInfo);?>;
$('#coursesDrop').change( function(){
$('#targetdiv').hide();
var courseId = $(this).val();
if (courseId !== '') {
for (var i = 0, l = courseinfo.length; i < l; i++)
{
if (courseinfo[i].CourseId == courseId) {
var currentindex = $('#currentDuration').val(courseinfo[i].Duration);
var text = $(this).find('option:selected').text();
var split = text.split(' - ');
$('#currentId').val($(this).find('option:selected').val());
$('#currentCourseNo').val( split[0] );
$('#currentCourseName').val( split[1] );
break;
}
}
}
else{
$('#currentCourseNo,#currentCourseName,#currentDuration,#currentId,#studentselect').val('');
}
});
$('#courseForm').delegate('change','select',(function(warnings)
{
return function()
{
warnings.html('');
};
}($('#warnings'))));
});
Look at this jQuery function:
$.parseJSON();
When you define the var "courseinfo" the php is returning a string (in a json format). You need to transform this string in an object (or array) to easily access the content.
If your json_encode($courseInfo) returns proper json string.
you can simply use JSON.parse to turn the string into usable javascript object
var courseinfo = JSON.parse('<?php echo json_encode($courseInfo);?>');