[英]How to display records based on selected year from dropdown dynamically without any submit button
I am working on PHP code-igniter. 我正在研究PHP代码点火器。
My view code goes like below: 我的查看代码如下所示:
<form method="post" action="">
<div class="col-md-4 col-sm-4 ">
<div class="form-group">
<label class="h5" for="Mode"><b>See year wise commitments</b></label>
<select class="form-control" name="Year" id="Year" onchange="this.form.submit()">
<option value="">Select Year</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
</select>
</div>
</div>
</form>
Just by selecting any year from dropdown, I have to pass the selected year to my controller and inturn to model and get the query results based on the selected year. 只需从下拉列表中选择任何年份,我就必须将所选年份传递给我的控制器,然后进行建模并根据所选年份获得查询结果。
In my controller I am receiving the posted value like: 在我的控制器中,我收到以下发布的值:
$selectedYear = $this->input->post('Year');
And before selecting any dropdown value the records in my view has to be of Current year. 在选择任何下拉值之前,我认为记录必须是当年的。
My ajax code is 我的ajax代码是
$( "#year" ).change(function() {
year = $(this).val();
$.ajax({
url : "controllerfilename/controllername",
data : {"year": year},
type : "post",
success: function(data){
console.log(data);
}
});
}); });
You can make an ajax call to your controller on change function of dropdown 您可以在下拉菜单的更改功能上对控制器进行ajax调用
$( "#year" ).change(function() {
year = $(this).val();
$.ajax({
url : "controller url",
data : {"year": year},
type : "post",
success: function(data){
console.log(data);
}
});
});
You defined id as id="Year"
but in the ajax you call it like $( "#year" ).change(function() {
. Means In ajax its year
and in view its Year
(uppercase). 您将id定义为
id="Year"
但在ajax中将其命名为$( "#year" ).change(function() {
。意味着在ajax中,其year
而在Year
大写。
Try this code: 试试这个代码:
$("#Year").change(function() {
year = $(this).val();
$.ajax({
url : "<?php echo base_url('controllerfilename/controllername') ?>",
data : {"year": year},
type : "post",
success: function(data){
console.log(data);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.