rofavadeka
rofavadeka

Reputation: 587

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++;
        } 
    }
?>

Upvotes: 0

Views: 1588

Answers (2)

Gor
Gor

Reputation: 505

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.

Upvotes: 1

djjjuk
djjjuk

Reputation: 358

should $i be replaced with $position:

$sql = "UPDATE table SET position = ".$i." WHERE id = ".$item;
$sql = "UPDATE table SET position = ".$position." WHERE id = ".$item;

in any case ... if it doesnt update the database presumably you get an error?

and, i assume your table isnt actually called 'table' otherwise you should probably backtick it..

Upvotes: 0

Related Questions