jQuery: What to do with the list that sortable('serialize') returns?

bart picture bart · Mar 17, 2009 · Viewed 43.4k times · Source

With jQuery I'm retrieving positions of a sortable list using 'serialize', like this:

var order = $('ul').sortable('serialize');

The variable 'order' then receives the following:

id[]=2&id[]=3&id[]=1&id[]=4&id[]=5

Now how can I use this data in an ajax call?

This is how I plan to do it, but it's ugly and I can't change the parameter name 'id':

$.post('ajax.php?'+order,{action:'updateOrder'});

Maybe I need to unserialize, then implode the variable 'order' and assign it to just one parameter?


I don't have a problem with the server side code, but I have a problem with the jQuery client site code. The question is, where do I place the 'order' variable in the script?

In the example I gave I added it as a query string:

'ajax.php?'+order

But I would like to pass it as a parameter, just like the action parameter. The following doesn't work, it returns a syntax error:

$.post('ajax.php?'+order,{action:'updateOrder',order});

Answer

bart picture bart · Mar 20, 2009

Found it! I needed to add the option key:'string' which changes the variable name to 'string' instead of 'id'.

var order = $('#projects ul').sortable('serialize',{key:'string'});

$.post('ajax.php',order+'&action=updateOrder');