I have two selectlists in my html and both have the values saved in database, I want to know how to change values in selectlist 2 when select 1 is changed by user using jquery?
I am trying to do these things in codeigniter, so please help me, I have done these things using jquery before but that was in simple php by passing the values to the load function of jquery, but this is not working in codeigniter, anyone please help me
Thanks,
Shah RUkh
Say for example you have this drop down
<select id="select_1">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select id="select_2">
<option value="">Select</option>
</select>
On change of select_1 drop down you should get the value and do a AJAX request to your controller function which will return you the options for select_2 than simply replace the html of select_2 with the returned response from script
$('#select_1').change(function(){
var id = $(this).val();
$.ajax({
type: "POST",
url: base_url+'controller/action/',
data: 'id='+id,
success: function(html){
$('#select_2').html(html);
}
});
});
For codeignitor use the base_url in the url parameter of ajax request. You can define a CDATA
variable for base_url which you can use in your ajax requests, you can define it in your header.php file.
<script type="text/javascript">
//<![CDATA[
base_url = '<?php echo base_url();?>';
//]]>
</script>
Okay I have done it, using jQuery
<script type="text/javascript">
$(document).ready(function()
{
$("#slect_1").change(function()
{
id = $("#select_1").val();
$("#select_2").load("<?php echo site_url('class/method'); ?>/"+id);
});
});
</script>
Thanks,
Shah Rukh