将Ajax变量传递给Codeigniter函数

Mat*_*att 4 jquery codeigniter

我认为这很简单.

我有一个Codeigniter函数,它从表单中获取输入并将它们插入到数据库中.我想Ajax化这个过程.目前函数的第一行从表单中获取id字段 - 我需要更改它以从Ajax帖子(它引用包含必要值的表单中的隐藏字段)获取id字段.我该怎么办?

我的Codeigniter控制器功能

function add()
{
    $product = $this->products_model->get($this->input->post('id'));

    $insert = array(
            'id' => $this->input->post('id'),
            'qty' => 1,
            'price' => $product->price,
            'size' => $product->size,
            'name' => $product->name
        );

    $this->cart->insert($insert);
    redirect('home');
}
Run Code Online (Sandbox Code Playgroud)

和jQuery Ajax函数一样

$("#form").submit(function(){
        var dataString = $("input#id") 
        //alert (dataString);return false;  
        $.ajax({  
            type: "POST",  
            url: "/home/add",  
            data: dataString,  
            success: function() {

            }  
        });
        return false;
    });
Run Code Online (Sandbox Code Playgroud)

一如既往,非常感谢提前.

Tee*_*eej 5

   $("#form").submit(function(){
        var dataString = $("input#id") 
        //alert (dataString);return false;  
        $.ajax({  
            type: "POST",  
            url: "/home/add",  
            data: {id: $("input#id").val()},  
            success: function() {

            }  
        });
        return false;
    });
Run Code Online (Sandbox Code Playgroud)

注意ajax方法中的数据选项.现在你可以$this->input->post('id')像在控制器方法中那样使用.