php Codeigniter ajax使用ajax代码向控制器发送数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18529070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter ajax send data to controller using ajax code
提问by Charlie
<script type="text/javascript">
$(document).ready(function () {
$("#select-dept").change(function () {
var id = $("#select-dept").val();
$.ajax({
type: "POST",
url: "<?=base_url()?>.index.php/sms/get_dept_employee",
//url: baseurl + 'sms/get_dept_employee',
data: "id",
dataType = "json",
cache: "false",
success: function (emp_list) {
$("#dept-emp").html(emp_list);
}
});
});
});
</script>
I am unable to send view data to controller function In view their is select box with departmenr values from mysql database
我无法将视图数据发送到控制器功能在视图中,它们是带有 mysql 数据库中的部门值的选择框
<select class="select-dept" id="select-dept" name="select-dept">
<option>Select Department</option>
<?foreach ($departments as $dt):?>
<option value="<?=$dt['id']?>">
<?=$dt[ 'name']?>
</option>
<?endforeach;?>
</select>
i need to get refresh when user select department and that need to call controller function get_dept_employeeAnd need to display datagrid of employee list
我需要在用户选择部门时刷新,并且需要调用控制器函数 get_dept_employee并且需要显示员工列表的数据网格
回答by bipen
you need to send data option as object
您需要将数据选项作为对象发送
try this
尝试这个
.....
url: "<?=base_url()?>.index.php/sms/get_dept_employee",
data: {"id":id},
dataType:"json",
....
and get the posted value as id
in your controller..
并获取id
在您的控制器中发布的值..
$id=$this->input->post('id');
....
回答by RONE
var id = $("#select-dept").val();
data:"id", //Here you are sending string as id
should be
应该
data:id, //No double quotes surrounded
回答by Nil'z
Try:
尝试:
data: {'id':$(this).val()},
回答by Kanishka Panamaldeniya
i saw few errors
我看到了一些错误
use data: {'id':id},
instead of data: "id",
使用data: {'id':id},
代替data: "id",
use dataType:"json",
instead of dataType="json",
使用dataType:"json",
代替dataType="json",
use cache:false
instead of cache: "false",
使用cache:false
代替cache: "false",
<script type="text/javascript">
$(document).ready(function () {
var base_url = "<?php echo $this->config->item('base_url'); ?>";
$("#select-dept").change(function () {
var id = $(this).val();
$.ajax({
type: "POST",
url: base_url+"index.php/sms/get_dept_employee";
data: {'id':id},
dataType:"json",
cache: false
success: function (emp_list) {
$("#dept-emp").html(emp_list);
}
});
});
});
</script>