Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I want an option to be selected in the course dropdown if it equals a certain value.

Course dropdown will be randomly populated each time one select a program from the program dropdown.

I have two dropdown boxes program and course, when I select the program the corresponding courses come in the course box, now I want the option selected in the courses whose id is posted.

Please help me to resolve the issue.

What I have tried:

PHP
<div class="form-group">
	<label for="program">Program</label>
	<select class="form-control" name="programid" id="programid" required>
	  <option value="">Select Program</option>
	  <?php foreach($programs as $obj) { ?>
		<option value="<?=$obj['programid'] ?>"<?php if($row['programid'] == $obj['programid']) { ?> selected <?php } ?> ><?=$obj['program'] ?></option>
		<?php } ?>
	</select>
  </div>
  <div class="form-group">
	<label for="course">Course</label>
	<select class="form-control" name="courseid" id="courseid" required>
	</select>
 </div>


JavaScript
$('#programid').change(function() {
      var programid = $(this).val();
      var courseid = null;
      <?php if($row['courseid'] != null && !empty($row['courseid'])) { ?> courseid = <?php echo $row['courseid']; }  ?>       
      $.ajax({
        url : "<?=site_url('get_courses');?>",
                    method : "POST",
                    data : {programid: programid},
                    async : true,
                    dataType : 'json',
                    success: function(data){                       
                        var html = '<option value="">Select Course</option>';
                        var i;
                        for(i=0; i<data.length; i++){
                            if(data[i].courseid == courseid)
                            {
                                html += '<option value='+data[i].courseid+' "option:selected" >'+data[i].course+'</option>';
                            }
                            else
                            {
                            html += '<option value='+data[i].courseid+'>'+data[i].course+'</option>';
                            }
                        }
                        $('#courseid').html(html);

                    }
      })
    });
Posted

1 solution

Quote:
JavaScript
html += '<option value='+data[i].courseid+' "option:selected" >'+data[i].course+'</option>';
Your generated HTML is invalid - you are adding the invalid attribute "option:selected" to your option element. The attribute is simply called selected, and should not be wrapped in quotes:
JavaScript
html += '<option value='+data[i].courseid+' selected>'+data[i].course+'</option>';
 
Share this answer
 
Comments
nyt1972 7-Feb-24 5:10am    
Thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900