Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reordering items in an ordered list using Jquery, Ajax, PHP & Mysql

Posted on 2012-03-16
3
Medium Priority
?
1,486 Views
Last Modified: 2012-03-20
I'm currently developing a site using PHP/MySQL which is also using Jquery pretty extensively, and I need to add in functionality that allows the user to re-order items within an ordered list using drag and drop.  

When list items are dragged and dropped to a new position within the ordered list -- I'd like to trigger an ajax function that updates the order of those list items in the MySQL database.  My ordered list will have a structure similar to the following (where the dynamic record ID values will be appended each list items ID attributes):

<ul id="task_list_55">
     <li id="task_39"></li>
     <li id="task_40"></li>
     <li id="task_41"></li>
     <li id="task_42"></li>
</ul>

I've achieved the functionality that I've described using Prototype & Scriptaculous in the past -- though I'd much prefer to use JQuery to accomplish this if possible.  How would I accomplish something this using JQuery, PHP, and MySQL ?

One last note -- there will be multiple different ordered lists on the same page -- each with their own unique ID attributes assigned to them -- and each of those ordered lists will need to be sortable.  Please advise on how I would accomplish this.

Thanks!
- Yvan
0
Comment
Question by:egoselfaxis
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Ahmed Merghani earned 1000 total points
ID: 37730190
I suggest to do such thing using jQuery UI. See this link:

http://jqueryui.com/demos/draggable/#sortable

After the ordering, you can handle the changes and trigger the AJax call within these events:
http://jqueryui.com/demos/draggable/#events
0
 

Author Comment

by:egoselfaxis
ID: 37738967
I'm already aware of jquery's ability to do drag n' drop using unordered lists.

I was actually looking for examples of the ajax function triggering (which somehow extracts the record ID values from each list item's ID attribute and passes them to the PHP script that does the re-ordering).  

Are you aware of any tutorials on this (or plugins) that might be helpful?

Thanks,
- yg
0
 

Author Comment

by:egoselfaxis
ID: 37743404
I ended up figuring this out myself:

$(function() {

	$( "ul.tasklist" ).sortable({	
		revert: true,
		stop: function(event, ui) { 
		
			document.getElementById("page").style.opacity = ".15";
			document.getElementById("page").style.filter = "alpha(opacity=15)";
			document.getElementById("loading").style.display = "block";		

			var ul_div_name = "#" + $(this).attr('ID');			
			
			var num_list_items = $(ul_div_name).children().length;
			
			var stage_ul_id = $(this).attr('ID');
			
			var stage_id = stage_ul_id.substring(stage_ul_id.indexOf('_')+6);	
			
			var list_of_task_ids = new Array();
			
			var num = 0;

			$(ul_div_name + ' li').each(function() {
			
				var id = $(this).attr('id').split('_')[1];
				
				list_of_task_ids[num] = id;	

				num++;	
				
			});				
			
			$.ajax({

				type : 'POST',
				url : '../user/packages.php?action=reorderTasks',
				dataType : 'json',
				
				data: {
					stage_id : stage_id,
					num_list_items : num_list_items,
					list_of_task_ids : list_of_task_ids,
				},
				 
				success: function(data) {
				
					document.getElementById("page").style.opacity = "1";
					document.getElementById("page").style.filter = "alpha(opacity=100)";
					document.getElementById("loading").style.display = "none";	
					
					alert(data.msg);		
					
					$('#package_edit').before('<div class="successtext"><input name="dismiss" type="button" class="deletebutton floatright" value="X" onclick="dismissWindow()" /><p><strong>The Tasks have been reordered successfully</strong></p></div>');
					
					$(".successtext").delay(1000).fadeOut(2000);

				},
				 
				error : function(XMLHttpRequest, textStatus, errorThrown) {
				 
					alert('ERROR!');
						
				}
				
			});	
		
		}	
		
	});	
	
});

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question