Let’s make sortable lists that remember their order like the ones found in the To-Do lists for 37signals’s Basecamp. With Ajax calls and sortables implemented in Prototype and script.aculo.us, this is just a matter of putting the puzzle pieces together. And for those of you impatient, here’s the final result.
First off, you’ll need the Prototype and script.aculo.us libraries. The documentation is relatively good, especially for Prototype. Include these in your file.
<script type="text/javascript" src="path"></script>
Remember that you need to include Prototype before script.aculo.us, a common first time mistake.
So in this tutorial, we’re going to sort list elements, but technically you could sort anything that the Sortable.create function of script.aculo.us supports.
Make a new list (with an id) and notice specifically the id of the elements. They have to be in the form string_identifier, where identifier is a unique number, for Sortable.serialize to work. You will probably want to retrieve each element from a database where the identifier would be the unique id. Also, your database table should contain a column that saves the order. For this example, we’ll call this column order_column and the list list_to_sort. Creative names. Here’s some pseudocode.
You can insert some entries into your table to check that everything works so far. Next, we’ll liven this up with Sortable.create.
Sortable.create("list_to_sort", { onUpdate: function() { new Ajax.Request("file_to_call.php", { method: "post", parameters: { data: Sortable.serialize("list_to_sort") } }); } });
Okay, let’s break this down. Check the documentation on Sortable.create for more information. The syntax is Sortable.create(id_of_list, parameter_hash); One of the parameters is a callback function onUpdate that is called when the user changes the order of the list and finishes the mouse click. Without this callback, the list will work on the client-side but nothing would be saved. So we’re going to make an Ajax POST request to the file file_to_call.php with the Prototype function Ajax.Request. Again, the documentation contains more useful information. But for here, we’ll need to specify a PHP file, which we will make later to interact with the database. We’ll send it as a POST request with a parameter data, the variable string that contains the current order of the list.
Sortable.serialize creates a string like: list_to_sort[]=2&list_to_sort[]=1&list_to_sort[]=3 where the numbers are the identifier parts of the list element ids after the underscore. Perfect.
Now we need to code file_to_call.php, which will take this string, passed as $_POST[’data’] and execute it.
<?php ?>
There’s a PHP function you may not have seen before. parse_str parses a strings into variables, somewhat similar to executing the string. So for the above string, parse_str will result in a variable $list_to_sort that contains a 3-element array (2, 1, 3).
Now, all we have to do is update the database so the order is actually saved.
<?php // SQL Query: // UPDATE `table` SET `order_column` = $i WHERE `id` = $list_to_sort[$i] } ?>
And you’re done! Assuming all went well, your sortable list should remember its order in all its Ajax goodness. Again here’s the working final result for your viewing pleasure. Enjoy.

Great thanks Paul! It exactly what I needed for admin part! It's more than SUPER!
This's great!!!! Im very happy with it.
Thank you for sharing it.
but,
Im wondering something more.
How can i check value of $_POST['data'] it?.
I want to change even name of menu with ajax.
not just sorting.
Can you help me paul?
Awesome! Thanks a lot for this tutorial, it's really simple and allowed me to make some great list sorting tools!
Thank you SO much for providing this - it's exactly what I needed. You are a wonderful person!
very good.
It's what I need,Thank youvery much!
thank you so much for this tutorial. i have my list items sorting now which is great. just one problem: the ajax end of things doesn't seem to be working. also perhaps i should note that one your demo the ajax end of things doesn't seem to be working either. no matter how quickly i refresh the page the items are always back to the same way they were before i resorted them. just letting you know. okay take care and thanks again!
Thanks for this easy to follow tutorial. I've not dabbled with draggables before now - but you have definitely shown me how easy it is! thanks again!
molar es mucho
The demo doesn't save the results to the database. On refresh it goes back to it's original order.
Thank you very much.
Simple, straight to the point.
Wow,
Very nice thank you
Very nice thank you
Where are you putting the "Now, all we have to do is update the database so the order is actually saved." code?
Is that going into the "filetocall.php" file? Or somewhere else...Love some help on this...All the DRAG/DROP is working great, but I'm a little lost on how exactly to save the data.
Great!
asdfasdf
Thanks a bunch.
Works great!
Great - thanks for this.
Worth emphasising that the <li> "id" has to be of the form stringnumber (eg a1, a_2) and not (a1,a2) or it won't work. Took me a while to figure it out, but it is stated in the Scriptaculous doc for Sortable.
oop .. previous post didn't display the way I expected.
The important thing to note was that the <li> "id" has to be formatted as string-underscore-number, otherwise it doesn't work.
Great tutorial, however, your example assumes that you have entries in the database which have an id of 1, 2, 3 and whose position can be ordered 1,2,3 etc.., but what if you need to identify the list items in a different way?
Supposing the items from the database had ids of lets say 8, 9, 10 and their positions rather 1,2,3 then your example would not work because you would not be updating the right entries in the database..
I got this to work in Rails with a bit more work, with any given subset of rows from the database.
For example, let's say you have 4 elements in the list, and their ids in the desired order are {9, 15, 8, 25}, after drag'n'drop. First, use this "desired order" list, POSTed to the server, to get the rows from the database, ordered by their existing ordercolumn values (I use a descending order).
In the database, let's say their "existing order" is {15, 25, 9, 8}, when ordered by their respective ordercolumn's, which are {25, 15, 9, 8}...
Loop through both arrays, the desired order and the existing order. For row id=9, update ordercolumn = 25. For row id=15, update ordercolumn = 15. For row id=9, to ordercolumn = 9. Then for row id=25, update to ordercolumn = 8.
That should put them in the correct order and maintain the uniqueness of the order_column, just as id's should be unique.
Oh, I forgot to mention... This assumes the html ids of the li elements are based on the database ids. Rails does this automatically with content_tag_for :li, @post, or similar... So post _ 9, post _ 15, post _ 8, and post _ 25...
Good WORK.... my list now is perfect. This a simply and powerful sorting tool!
Thank You!
Does anyone know of a ColdFusion example of this?
I was trying to copy this and it wont update the database. it seems everything is in place and the sort works on the page but it doesnt update the DB. any help would be appreciated.
Sorry Brain FART!! :) helps to have database connectivity.. :)
This is great. After the database has been updated with new list order, how can you update outputted data on the page with the list so that it reflects the updated info? For example, if your list was numbered, how would you update/refresh those numbers once an item has been moved without refreshing the whole page?
Thanks for writing a clear, easy to follow tutorial for this. The scriptaculous document is short on practical applications, especially for newbies.
Wow, finally a nice simple tut with very clear examples and explanation. Why can't all tuts be like this? Thanks much!
My comment !!!
This is a short an amazing tutorial that strikes to the point and deserves many thanks!! :)
superrrrrrrrrrr
q2yPZQ Cool lol hey bla bla bla bla
318ayY Cool lol hey bla bla bla bla
wVmH9b Cool lol hey bla bla bla bla
This very cool and was really very helpful
This was very cool and really very helpful
this was very good,nice script.
This is [qeehu.com](http://www.qeehu.com/ "qeehu") my website.
well done!thanks.<a href="http://www.51guest.com/">builder</a>my site
THX that's a great anwser!
EpwKIa <a href="http://cyjmnkfxemzo.com/">cyjmnkfxemzo</a>
I need to save sortable list.. so when we sort something will save as the new sort..
somebody help me??