rofavadeka rofavadeka - 4 months ago 23
Javascript Question

jQuery UI Sortable and updating a mysql database

I am doing something wrong and it's driving me nuts because I can't figure it out.
Using jQuery ui sortable to sort the divs on my page. The sorting part works kinda but it does not update the database.

The only thing I can think of is the clear within page, but if I put this on the div it acts not as fluent.

PHP

<div id="page">
<div id="listItem_'.$id.'" class="a bunch of random classes">
<div class="handle"></div>
</div>
<div id="listItem_'.$id.'" class="this one has some other classes">
<div class="handle"></div>
</div>
<div class="clear"></div>
<div id="listItem_'.$id.'" class="a bunch of some other">
<div class="handle"></div>
</div>
</div>


Javascript

$(document).ready(function(){
$("#page").sortable({
handle : '.handle',
update : function () {
var order = $('#page').sortable('serialize');
$(document).load("sort.php?"+order);
}
});
});


sort.php

<?php
session_start();
require('connect.php');

if($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_SESSION['USERNAME'])){
$i=1;
foreach ($_GET['listItem'] as $position => $item){
$sql = "UPDATE table SET position = ".$i." WHERE id = ".$item;
$res = mysql_query($sql);
$i++;
}
}
?>

Gor Gor
Answer

Start debugging by placing var_dump('got here'); (or one of the variables) in your PHP script. Use the console in Firebug (which is an add-on) in Firefox to view the output. Step through until you find the spot where it's failing.