Draging and Droping

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Draging and Droping

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Draging and Droping

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Re: Draging and Droping

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Draging and Droping

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Draging and Droping

Post by Benjamin »

Why do you have an "ORDER BY" clause in an "UPDATE" query? Is that query even working without an error?
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Re: Draging and Droping

Post 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++;
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Draging and Droping

Post 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++;
}
 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Draging and Droping

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Draging and Droping

Post by Benjamin »

Turn on error reporting and display errors too.
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Re: Draging and Droping

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Draging and Droping

Post 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>';
 
Post Reply