I am trying to use jQuery draggable/droppable to drag a list item to a div, upon which the list item will be deleted from the draggable ul and an ajax call will be made to delete the corresponding item in a MySQL database.
The HTML:
<ul id="draggable">
<li id="list_1">List item 1</li> // Incrementing ID nos. from database
<li id="list_2">List item 2</li>
<li id="list_3">List item 3</li>
</ul>
<div id="droppable"></div>
The jQuery so far:
$('#draggable li').draggable({
drag: function(event, ui) {
var id = $(this).attr('id').split("_");
id = id[1]; // OK so far - but how to pass this to droppable?
}
});
$('#droppable').droppable({
drop: function(event, ui) {
// How to get the id from draggable here so the correct list item is acted on?
$('#draggable li').remove();
$.ajax({
url: "delete.php",
type: "GET",
data: {
'id': id,
'user_id': user_id // user_id comes from PHP
}, // end ajax }
});
UPDATE
Many thanks for the responses. I got it to work using:
<script>
var user_id = "<?php print($user_id); ?>";
$(document).ready(function() {
$('#draggable li').draggable({
helper: 'clone'
});
$('#droppable').droppable({
accept: '#draggable li',
drop: function(ev, ui) {
var id = $(ui.draggable).attr('id').split("_");
var movie_id = id[1];
$('#draggable li#list_' + id).remove();
$.ajax({ // begin add ajax
url: "delete.php",
type: "GET",
data: { // begin add data
'id': id,
'user_id': user_id
} // end data
}); // end ajax
} // end drop function
}); // end droppable
}); // end ready
</script>
It seeems OK to me - are there any problems with that which you can see?
You can access the dragged element using using ui.draggable.attr('id')
inside your drop function.