Chad
Chad

Reputation: 163

Using jQuery serialize with CodeIgniter function

I need some help with jQuery and serialize.

I am not very good with javascript so this might be such a simple problem that i am doing wrong but I am working on an application within codeigniter and one of the features is a drag and drop system which allows you to re-order the images and then save the order and update the order/weight in a MySQL database. Now, I can drag and drop the images fine, press the button which calls that save_order function and the success alert shows but nothing actually happens and i'm clueless as to why. Like I said, i am completely new to javascript/jQuery so it is probably something so simple. Any help would be greatly appreciated.

This is my javascript:

$(function() {
    $( "#imgs" ).sortable();
});

function save_order(){   
    $.ajax({
        url: "<?php echo site_url('admin/gallery/change_order'); ?>",
        type: 'POST',
        data: $("#imgs").sortable("serialize"),
        success: function(msg){
            alert('Success');
        },
        error: function (XHR, status, response) {
            alert('An error occurred!');
        }
    });             
}

and this is gallery/change_order

public function change_order()
{
    $items = $this->input->post('car');

    foreach($items as $key => $value)
    {
        $data = array('weight' => $value);

        $this->db->update('images', $data, array('id' => $key));
    }
}

Upvotes: 0

Views: 775

Answers (1)

Oscar P&#233;rez
Oscar P&#233;rez

Reputation: 4397

I think you have an error in the way you call 'serialize'. It should be something like:

        data: $("#imgs").serialize(),

Keep in mind that 'serialize' will work only on form elements.

Upvotes: 1

Related Questions