I want to retrieve value from database based on dropdown value using the ajax.
Here I'm attaching the screenshot thats what exactly I need. Once we change the year the result should be changed.
3 Answers
<script type="text/javascript">
jQuery(document).ready(function(){
$("#district").change(function() {
var vdcID = {"district_id" : $('#district').val()};
$.ajax({
type: "POST",
data: vdcID,
url: "<?php echo base_url(); ?>admin/ajax/showVdcById",
dataType: 'json',
success: function(data){
$('#vdc').html("<option>Please Select VDC</option> ");
$.each(data, function(i, data){
$('#vdc').append("<option value='"+i+"'>"+data+"</option>");
});
}
});
});
});
this code might help you. it takes value from from a dropdown and updates value on another dropdown based on the id it sends to database
Add onchange event updateMonths() to year select tag
function updateMonths()
{
var selectedYear = $('#year').val();
if(selectedYear!= "")
{
$.ajax({
type: "POST",
data: { "year":selectedYear },
url: "<?php echo base_url(); ?>admin/ajax/getAllMonths",
dataType: 'json',
success: function(data){
$.each(data, function(i, data){
$('#month').append($('<option>', {
value: i,
text: data
}));
});
}
});
}
}
1 Comment
Shihas
i don't want to update the value in month drop down. I just want to pass the value to my controller when we select any of the year.
There are two ways to do it:
- On drop down change you can reload the entire page with appending the year in url of the page and show your result on the basis of that url appended year value.
- You can use ajax call on change of year drop down and load the specific content in a div.
ajax method:
var dropdownSelection = $('#dropdown_id').val();
$.ajax({
url : 'proccess.php',
method : 'get',
data :
{
selection : dropdownSelection,
},
success : function(response)
{
$('#div_result').html(response);
}
});
