简体   繁体   English

Codeigniter ajax使用ajax代码将数据发送到控制器

[英]Codeigniter ajax send data to controller using ajax code

<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_employee And need to display datagrid of employee list 我需要刷新用户选择部门时需要调用控制器函数get_dept_employee并且需要显示员工列表的数据网格

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');
....
var id = $("#select-dept").val();     

 data:"id", //Here you are sending string as id

should be 应该

     data:id, //No double quotes surrounded 

尝试:

data: {'id':$(this).val()},

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM