微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将Ajax变量传递给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');
}

和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;
    });

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

解决方法

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

            }  
        });
        return false;
    });

注意ajax方法中的数据选项.现在您可以使用$this-> input-> post(‘id’),就像您在控制器方法中所做的那样.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐