javascript - 如何将 Ajax URL 中的两个变量传递给 codeigniter Controller ?

标签 javascript php jquery ajax codeigniter

我正在研究学校管理系统。我只是按类(class)和部门创建获取学生详细信息。我使用 ajax + codeigniter Controller ,但我无法在 ajax 调用中传递两个变量来执行 2 个参数搜索。

我的 Ajax 代码

<script>
                $(document).ready(function () {
                    $('#example').DataTable({
                        'paging': true,
                        'searching': true,
                        'ordering': true,
                        'autoWidth': false
                    });
                    $('#student').click(function (event) {
                        event.preventDefault();
                        var xclass = $('.sclass').val();
                        var section = $('.section').val();

                        //
                        $.ajax({
                            url: "<?php echo base_url(); ?>Admission/fetchStudent/",
                            type: "POST",
                            data: {'xclass': xclass, 'section':section},
                            datatype: 'json',
                            success: function (data) {
                                $("#resultlist").html(data);
                            }
                        });
                    });
                }); //event.preventDefault(); 

            </script>

我的 Controller

public function fetchStudent($class,$section){
    $this->load->model('Admission_model');
    $data = $this->Admission_model->fetchStudentmodel($class,$section);
    echo '<pre>';
    print_r($data);
    exit();
    echo json_encode($data);
}

我的模型是

public function fetchStudentmodel($x,$y) {
    $uid = $this->session->userdata('user_id');
    $data = $this->db->select('*')
            ->from('student')
            ->where(['user_id' => $uid,'class'=>$x, 'section'=>$y])
            ->get();

    if ($data->num_rows() > 0) {
        return $data->result();
    } else {
        return 'No data Available';
    }
}

看到这张图你就明白我想做什么了enter image description here

最佳答案

在您的情况下,您可以像这样在参数中传递 2 个值:

data: { key1: value1, key2: value2 }

您可以使用 $_POST

在您的 Controller 中获取它们

但在您的示例中,您只发送了 1 个参数而没有在您的 Controller 中获取它,您正在使用您的 URL slug 来获取类。

在您的 Controller 文件中,您可以简单地获取 $_POST 中的值,您可以使用以下方法检查这两个值:

echo '<pre>';
print_r($_POST);

您将在 $_POST 中获得这两个值,然后您可以使用 $_POST['key1']$_POST['key2'] 访问

还有一件事,我不知道你为什么要在你的参数键上使用单引号,我想这将被转换成一个字符串变量。

您的示例的第二个解决方案是: var xclass = $('.sclass').val(); var section = $('.section').val();

data: 'class='+xclass+'&section='+section, // here you can use quote and & for separation.

关于javascript - 如何将 Ajax URL 中的两个变量传递给 codeigniter Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55181233/

相关文章:

javascript - 如何从函数返回对象?

javascript - 如何使用 jQuery 检测用户是否几乎到达页面末尾?

javascript - HTML 5 的音频标签在 angularJS 中连续使用时不起作用

php - 将自定义订单状态设置为对付款有效

jquery - 单击下拉菜单时打开所有子菜单

javascript - 在 Vue.js Mounted() 中更新数据不起作用

php - 检查联系表是否有效(自动测试)

php - !$festive 与 $festive==FALSE 相同吗?

javascript - Datatables JS - 是否有一种 HTML 5 方法可以通过在 <th> 上指定数据顺序参数来对数据进行排序?

jquery - Html5 Canvas 模糊并保存