ctlockey
ctlockey

Reputation: 352

Update DB with jQuery sortable ajax call in CakePHP

I'm trying to simply reorder the rows in a table with jQuery's sortable. I have the drag/drop working fine, my problem is with saving the new row order my ajax call, specifically with sending the data to my action in CakePHP. I've searched and tried different things all day to no avail. Basically, nothing is updating the DB at all.

If I run my ajax call with an empty action, I can get the success: callback to alert something, which I understand to mean it's successfully hitting my action. Please correct me if I'm wrong there. Here's my jQuery and AJAX call:

$('#featured-items tbody').sortable({

    cursor: 'move',
    stop:function(i) {

        $.ajax({
            type: "GET",
            url: "/admin/features/reorder",
            data: $('#featured-items tbody').sortable("serialize"),
            success: function() {
                alert($('#featured-items tbody').sortable("serialize"));
            }               
        });

    }

}).disableSelection();

And my data I'm sending is:

item[]=2&item[]=1&item[]=24

Could someone help me understand how to access my data in the controller action? So far I have this (from looking at other examples of this). I'm using Cake 1.3 and my model is called Feature.

function admin_reorder()
{
    $this->autoRender = false;

    if($this->RequestHandler->isAjax())
    {
        foreach($this->data['item'] as $order => $id)
            $this->Feature->id = $id;
            $this->Feature->saveField('priority', $order);
    }
}

Upvotes: 0

Views: 2216

Answers (2)

thanat
thanat

Reputation: 334

try use $_GET/$_POST instead of $this->data :

function admin_reorder()
{
    $this->autoRender = false;

    if($this->RequestHandler->isAjax())
    {
        foreach($_GET['item'] as $order => $id)
            $this->Feature->id = $id;
            $this->Feature->saveField('priority', $order);
        }
    }

}

Upvotes: 1

Hoff
Hoff

Reputation: 1772

To my knowledge, $this->data is only for posted data, whereas in your AJAX call you're using the GET method. Try changing your method to POST:

$('#featured-items tbody').sortable({

    cursor: 'move',
    stop:function(i) {

        $.ajax({
            type: "POST",
            url: "/admin/features/reorder",
            data: $('#featured-items tbody').sortable("serialize"),
            success: function() {
                alert($('#featured-items tbody').sortable("serialize"));
            }               
        });

    }

}).disableSelection();

Upvotes: 0

Related Questions