SuperNinja
SuperNinja

Reputation: 1596

Codeigniter and data from ajax request

I've been playing with this for a while and not able to get it to work. I am using CodeIgniter and on click of button $('.galName').click(initUpdate); I am calling function:

function initUpdate()
{
    var id = $(this).attr('id').split('_')[1];
    //grab id from link update_xxxx

    //get gallery information
    $.ajax(
    {
        type: 'POST',
        url: 'ajax/update',
        data: id,
        dataType: 'json',
        success: function(json)
        {
            alert(json);
        }
    });
}

I can alert the id after var is set and that's correct. Here is the function called within CodeIgniter controller.

public function update()
        {
            $id = $this->input->post('id');

            echo json_encode($id);
        }

If I set $id="something" and alert(json) it works fine and alerts whatever I set something to. However, my issue is with $id = $this->input->post('id'); It's not pulling my data from the ajax request. This must be the incorrect way of doing within CodeIgniter and I haven't been able to track down the solution otherwise. Any help would be much appreciated.

Thanks!

Upvotes: 0

Views: 2075

Answers (1)

Musa
Musa

Reputation: 97672

You did not set the key part of the key/value pair, try

function initUpdate()
{
    var id = $(this).attr('id').split('_')[1];
    //grab id from link update_xxxx

    //get gallery information
    $.ajax(
    {
        type: 'POST',
        url: 'ajax/update',
        data: {"id": id},// set the "id" key
        dataType: 'json',
        success: function(json)
        {
            alert(json);
        }
    });
}

Upvotes: 1

Related Questions