Page 1 of 1

Draging and Droping

Posted: Fri Jan 30, 2009 11:45 am
by QbertsBrother
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


hello

i have a page that has some javascript on it and it allows me to drag and drop the order of items. kind of like the netflix queue. now i am having trouble updating the records in the database to reorder them.

here is the javascript i am using

Code: Select all

<script type=\"text/javascript\" language=\"javascript\">   
   
   function updateOrder(){
        var ampcharcode= '%26';
        var serializeOpts = Sortable.serialize('item_list')+ unescape(ampcharcode)+\"key=item_list\"+unescape(ampcharcode)+\"update=directors\";
        var options = {
            method : 'post',
            parameters : serializeOpts,
            };
            
           alert(options.parameters);
           new Ajax.Request('reorder.php',options);
   }
 
  Sortable.create('item_list',{tag:'tr', ghosting:true,constraint:'vertical', onUpdate : updateOrder,tree:true})
 
</script>
 
i believe it is posting things. i can get it to update but i cant get it to enter the correct values in the database.

here is what i am doing to reorder

Code: Select all

if(isset($_POST['update'])){
 
foreach($_POST['item_list'] as $key => $value){
$displayorder = $_POST['id'];
$qUpdateDirectors = "update board_members set display_order = '$displayorder' where id = '1' order by display_order";
$rUqUpdateDirectors = mysql_query($qUpdateDirectors) or die (mysql_error());
}
 
}
 
and here is what i think it is posting
 

Code: Select all

item_list[0][id]=1&item_list[1][id]=6&item_list[2][id]=5&item_list[3][id]=2&item_list[4][id]=4&item_list[5][id]=3&item_list[6][id]=7&item_list[7][id]=8&item_list[8][id]=9&item_list[9][id]=10&item_list[10][id]=11&key=item_list&update=directors
any help with this would be great. i have been trying all morning and cant get it to work. i am going crazy.

thanks


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Draging and Droping

Posted: Fri Jan 30, 2009 2:18 pm
by pickle
What you think it's posting and what its actually posting may be 2 completely different things. Start by doing a dump of $_POST.

Re: Draging and Droping

Posted: Fri Jan 30, 2009 2:32 pm
by QbertsBrother
i tried to echo the post but what it does here

Code: Select all

          alert(options.parameters);
           new Ajax.Request('reorder.php',options);
i believe that ajax is posting the values to reorder.php and i cant see what is being posted

but the alert function returns this

Code: Select all

item_list[0][id]=6&item_list[1][id]=1&item_list[2][id]=5&item_list[3][id]=2&item_list[4][id]=4&item_list[5][id]=3&item_list[6][id]=7&item_list[7][id]=8&item_list[8][id]=9&item_list[9][id]=10&item_list[10][id]=11&key=item_list&update=directors

Re: Draging and Droping

Posted: Fri Jan 30, 2009 2:47 pm
by pickle
You're going to need to do something to figure out what's going on in that PHP file. Do you have any communication from the PHP file, back to your javascript? That's likely how you're going to have to debug this.

Re: Draging and Droping

Posted: Fri Jan 30, 2009 2:51 pm
by Benjamin
Why do you have an "ORDER BY" clause in an "UPDATE" query? Is that query even working without an error?

Re: Draging and Droping

Posted: Fri Jan 30, 2009 3:06 pm
by QbertsBrother
astions wrote:Why do you have an "ORDER BY" clause in an "UPDATE" query? Is that query even working without an error?
yeah i took that out. i copied another query and when i posted the message i had pasted the unedited query.

here is my new query

Code: Select all

$i=1;
foreach($_POST['item_list'] as $key=>$value) {
mysql_query("update board_members set display_order = '$i' where id = '$value'");
$i++;
}

Re: Draging and Droping

Posted: Fri Jan 30, 2009 3:10 pm
by Benjamin
That query will fail too as id is a reserved keyword. You need to place backticks around it.

Code: Select all

 
$i=1;
foreach($_POST['item_list'] as $key=>$value) {
mysql_query("update board_members set display_order = '$i' where `id` = '$value'");
$i++;
}
 

Re: Draging and Droping

Posted: Fri Jan 30, 2009 3:35 pm
by pickle
It looks like you haven't tested your PHP file very much. I'd suggest changing your AJAX request to GET rather than POST, figure out what the exact URL is that you're requesting (Firebug can help there), manually put that URL in your browser, and debug until it does what you want.

Guessing what's wrong based on an AJAX request that gets no feedback is, frankly, a waste of time.

Re: Draging and Droping

Posted: Fri Jan 30, 2009 3:50 pm
by Benjamin
Turn on error reporting and display errors too.

Re: Draging and Droping

Posted: Fri Jan 30, 2009 4:03 pm
by QbertsBrother
i am looking at the logs and i dont see any errors. even when i change my query to a query that i know will not work such as changing the table name to one that does not exist.

but when i remove this part from the javascript

Code: Select all

,tree:true
it updates a record. only one and it sets it to 1.

isnt this what the javascript is posting?

Code: Select all

item_list[0][id]=11&item_list[1][id]=1&item_list[2][id]=6&item_list[3][id]=5&item_list[4][id]=2&item_list[5][id]=3&item_list[6][id]=4&item_list[7][id]=7&item_list[8][id]=8&item_list[9][id]=9&item_list[10][id]=10&key=item_list&update=directors

Re: Draging and Droping

Posted: Fri Jan 30, 2009 4:06 pm
by Benjamin
Have you checked to see what is being posted by dumping the $_POST super global?

Code: Select all

 
echo '<pre>' . print_r($_POST, true) . '</pre>';