Passing an Ajax variable to a Codeigniter function

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-05-06T06:39:21Z Indexed on 2010/05/06 6:48 UTC
Read the original article Hit count: 241

Filed under:
|

Hello,

I think this is a simple one.

I have a Codeigniter function which takes the inputs from a form and inserts them into a database. I want to Ajaxify the process. At the moment the first line of the function gets the id field from the form - I need to change this to get the id field from the Ajax post (which references a hidden field in the form containing the necessary value) instead. How do I do this please?

My Codeigniter Controller function

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

And the jQuery Ajax function

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

            }  
        });
        return false;
    });

As always, many thanks in advance.

© Stack Overflow or respective owner

Related posts about codeigniter

Related posts about jquery-ajax