How do I populate a select list in jQuery with data from PHP? -
i have bunch of courses , each course has different number of lessons. so, have 2 select lists 1 courses , 1 lessons associated course. course select list easy, i'm doing end of php in foreach loop. i'm going through courses student has:
public function createcontent($student) { //get student courses //get lessons of each course //get upcoming lessons $lessons = $this->getlessons($courses); //need make use of $lessons populate lesson list foreach($coursearray $c) { $courseselectlist .= '<option id="select"'.$count.' value="'.$c['id'].'">'.$c['fullname'].'</option>'; $count++; } //create lesson select list in php backend or in front end dynamically jquery? return $courseselectlist.$lessonselectlist; }
my next step create second select list contains lessons of selected course. how list populated depends on student selects in course select list. dynamic , depends on student courses (and lessons) in course select list different each student , retrieved database. given student , course, know lessons should in lesson select list. lessons retrieved after first retrieve courses of student shown in comments in code below. need way populate lesson select list according student selects.
how should go doing this? great. bear in mind i'm beginner jquery user.
you need issue ajax request based upon value of selected course. assuming below id of course select 'courseselect':
$('#courseselect').bind('change', function() { var _t = $(this); var val = _t.val(); $.ajax({ url: '/path/to/script.php', data: 'c='+val, success: function(html) { $('#lessonselect').html(html); } }); });
the strategy behind 'script.php' called once different option selected courses. 'script.php' sent value of option browser , can accessed $_get['c']
. success function called once request returns, , html parameter set equal everything output inside of script.php (via echo
, print
, error statements issued php, all output).
in sense script.php select list of lessons database given course, , output of <option value = 'lesson key'>lesson</option>
markup. since output of server-side script html markup, named parameter success function html.
script.php
<?php // validate existence , proper type of input variable // assuming here 'value' option each course // integer primary key table in database if(!isset($_get['c']) || !is_numeric($_get['c'])) exit(); $course = intval( $_get['c'] ); // select of lessons selected course database // , output them html option elements. ?>
Comments
Post a Comment