简体   繁体   English

使用ajax将javascript数组传递给codeigniter中的控制器以与函数一起使用

[英]Using ajax to pass javascript array to a controller in codeigniter to use with a function

I have a form with a submit button. 我有一个带有提交按钮的表格。 When clicked, it adds listed elements to an array. 单击时,它将列出的元素添加到数组。

$("#submitButton").click(function(){
    var selection = $("#selectedList li");
    var families_selection = [];
    selection.each(function() {
        families_selection.push($(this).text().replace(/Remove/,''));
    });
 });

I want to use the array "families_selection" in the controller "Provider", so I can use the following function and instert the values in a database. 我想在控制器“提供程序”中使用数组“ families_selection”,因此我可以使用以下函数并将值插入数据库中。

$this->Proveedormodel->add_uk_proveedor_familia($idProveedor, $families_selection);

EDIT: I get the value for $idProveedor in the controller, not in the view, using another function in the same "Proveedor" model. 编辑:我在同一“ Proveedor”模型中使用另一个函数在控制器中而不是在视图中获得$ idProveedor的值。

$idProveedor = $this->Proveedormodel->get_idConsecutivo();

This is how I insert the values in the database in my model. 这就是我在模型中的数据库中插入值的方式。

function add_uk_proveedor_familia($id, $params){
    foreach($params as $clave){
        $this->db->select('id')->from('familia')->where('clave', $clave);
        $valor = $this->db->get();
        $vl = $valor->row_array();
        $familia = $vl['id'];
        $this->db->insert('relacionproveedorfamilia', array(
            'idProveedor' => $id,
            'idFamilia' => $familia
        ));
    }    
}

How can I use ajax to pass this array to the controller and use the function I need? 如何使用ajax将数组传递给控制器​​并使用所需的功能?

Edited code using Dragan Valjak's response. 使用Dragan Valjak的响应编辑代码。 Now it works! 现在可以了!

View add.php 查看add.php

$("#botonGuardar").click(function(){
    var seleccion = $("#listaSeleccion li");
    var familias_seleccion = [];

    seleccion.each(function() {
        familias_seleccion.push($(this).text().replace(/Quitar/,''));
    });

    $.ajax({
        url: 'Proveedor/crearRelacion',
        method: 'POST',
        data: {familias_seleccion: familias_seleccion}
    });
});

Controller Proveedor.php 控制器Proveedor.php

function crearRelacion(){
    $this->load->model('Proveedormodel');
    $familias_seleccion = $this->input->post($data);
    $idProveedor = $this->Proveedormodel->get_idConsecutivo();
    $this->Proveedormodel->add_uk_proveedor_familia($idProveedor, $familias_seleccion);
}

Model Proveedormodel.php 模型Proveedormodel.php

function add_uk_proveedor_familia($id, $params){
    foreach($params as $clave){
        $this->db->select('id')->from('familia')->where('clave', $clave);
        $valor = $this->db->get();

        $vl = $valor->row_array();
        $familia = $vl['id'];

        $this->db->insert('relacionproveedorfamilia', array(
            'idProveedor' => $id,
            'idFamilia' => $familia
        ));
    }    
}

You create your route to handle the ajax, something like '/family_selection' then you can pass your data that you have stored in a post method such as: 创建处理ajax的路由(类似于'/family_selection'然后可以传递存储在post方法中的数据,例如:

$.ajax({
    url: '/family_selection',
    method: 'POST',
    data: {family_data: variable_of_data_you_stored}
});

I'm not familiar with codeigniter but I think you would need to set up a route "something like" $route['family_selection']['post'] = '/family_selection'; 我不熟悉codeigniter,但我认为您需要设置一条路由“类似于” $route['family_selection']['post'] = '/family_selection';

In python I would set it up in my controller like so: Maybe you can use it as an example and implement something similar in codeigniter 在python中,我将像这样在控制器中进行设置:也许您可以将其用作示例,并在codeigniter中实现类似的功能

 @root_resource.route('/family_selection', methods=['POST'])
 def family_selection():
     family_details = request.form['family_data']
     #then I would do whatever it is I want to do with the data...
     #then I may or may not want to return an empty response.
     return Response(status=http.HTTPStatus.NO_CONTENT)

concentrate on your requirement id and family_data 专注于您的需求ID和family_data

     $.ajax({
                    url: "Proveedormodel/add_uk_proveedor_familia",
                    type: 'POST',
                    data:
                    {
                     family_data: variable_of_data_you_stored, //the data that you want to pass..

                     },
                    success: function(response)
                    {

                        console.log(response);


                    }
                  });

In model file: Proveedormodel and function add_uk_proveedor_familia() this changed ur params with family_data 在模型文件中:Proveedormodel和函数add_uk_proveedor_familia()更改了family_data的参数

function add_uk_proveedor_familia($id, $params){
$params=$_POST['family_data'];
    foreach($params as $clave){
        $this->db->select('id')->from('familia')->where('clave', $clave);
        $valor = $this->db->get();
        $vl = $valor->row_array();
        $familia = $vl['id'];
        $this->db->insert('relacionproveedorfamilia', array(
            'idProveedor' => $id,
            'idFamilia' => $familia
        ));
    }    
}

here in family_data your passing with large data if your params is the same as family_data then use use post value of family_Data into it. 如果您的参数与family_data相同,则在family_data中使用大数据传递,然后使用family_Data的use post值输入。

$.ajax({
    url: 'Provider/functionName',
    method: 'POST',
    data:
         {
         families_selection: families_selection
         }
});

In Controller

function functionName($data){
    $families_selection = $this->input->post($data);
    $idProveedor = $this->Proveedormodel->get_idConsecutivo();
    $this->Proveedormodel->
    add_uk_proveedor_familia($idProveedor,$families_selection);
}

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

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