I’m using jQuery UI to create a drag and drop function for a UL. The idea is that you can sort items within a list and the view order will update in the database (which is working). The function I’m trying to add now, is the ability to drag a LI into a different UL and have the database update so that the app knows which list the LI goes in. The item can be moved into a different list, but the php file I post to is not updating the database.
The PHP file echos the list like so:
<div class="list_div">
<h3>List #1</h3>
<ul id="listnumber_25" class="ui-sortable" unselectable="on">
<li id="item_134">list item 1</li>
<li id="item_135">list item 2</li>
<li id="item_136">list item 3</li>
<li id="item_137">list item 4</li>
</ul>
</div>
<div class="list_div">
<h3>List #2</h3>
<ul id="listnumber_26" class="ui-sortable" unselectable="on">
<li id="item_138">list item 1</li>
<li id="item_139">list item 2</li>
<li id="item_140">list item 3</li>
<li id="item_141">list item 4</li>
</ul>
</div>
And the JS looks like this:
//sort / change list
$(document).ready(function(){
$(".list_div ul").sortable({
receive: function(event, ui) {
var list_id = this.id;
var li_to_reassign = ui.item.attr('id');
$.post("functions/changelist.php", { li_to_reassign: li_to_reassign, list_id: list_id } );
}
});
});
functions/changelist.php:
<?
$list_id = $_POST['list_id'];
$list_id = substr('$list_id', 11);
$li_to_reassign = $_POST['li_to_reassign'];
$li_to_reassign = substr('$li_to_reassign', 5);
//------------------------------------------
// connect to DB
$host="localhost"; // Host name
$username="********"; // Mysql username
$password="********"; // Mysql password
$db_name="**********"; // Database name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="UPDATE tasks SET list_id='$list_id' WHERE id='$li_to_reassign'";
$result=mysql_query($sql);
?>
I can’t seem to find the problem!
in your php file you’re getting the value from
$_POSTbut straight after you reassing those variables with new value, using a variable$strthat it’s not even set:it should be:
EDIT (if you want to get a subtring of the
$_POSTvariables use the function substr like this):