如何将ajax上的数据传递给codeigniter控制器 [英] how to pass data on ajax to codeigniter controller

查看:28
本文介绍了如何将ajax上的数据传递给codeigniter控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择.我想将选定的选项值从 ajax 传递给我的控制器.但是,当我试图从我的控制器中回显它时,我得到的值为 null(所以我可以看到该值).

I have a select option. I want to pass the selected option value to my controller from ajax. But, I got the value is null when I'm trying to echo it from my controller (so I can see the value).

这就是我显示我的选择选项的方式:

this is how I display my select option :

<form class="form-horizontal" action="#">
    <select name="categories" onchange="ipilihCat()" id="categories" class="js-example-placeholder-single form-control" style="width: 200px;">
        <option value="">pilih category</option>
        <?php
        foreach ($cats as $cats) {
            echo '<option value="' . $cats['category'] . '">' . $cats['category'] . '</option>';
        }
        ?>
    </select>
</form>

这是我使用的ajax代码:

here is the ajax code that I use :

function ipilihCat(){
var selected = $('#categories').val();

$('#target').DataTable({
    "processing": true,
    "serverSide": true,
    "order": [],
    "ajax": {
        "data": {
            'cats': selected
        },
        "dataType": 'json',
        "url": "<?= base_url('welcome/iselCats'); ?>",
        "type": "POST",
        "success": function(data) {
            console.log(data);
        }
    },
    "columnDefs": [{
        "target": [-1],
        "orderable": false
    }],
});
}

在我的控制器中,我是这样检查的:

And in my controller, I check it like this :

public function iselCats(){
   $cats = $this->input->post('cats');
   echo $cats;
}

我觉得我从ajax传数据的时候有问题.我这是传递数据的正确命令吗?

I think there is a problem when I pass the data from ajax. I this the right command to pass the data?

"data": {
            'cats': selected
        },

推荐答案

试试下面的更改,已经用过几次了.

Try with the change below, have used it a couple of times.

"url": "<? echo base_url('welcome/iselCats'); ?>",

这篇关于如何将ajax上的数据传递给codeigniter控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆