unplugged_web Posted February 6, 2013 Share Posted February 6, 2013 I've found this drag and drop script and it's brilliant but I can't seem to get it to update the results in the database. I've got this on the page: <script language="Javascript">$(document).ready(function() { // Initialise the table$('#images').tableDnD({onDragClass: "drag",onDrop: function(table, row) { alert($.tableDnD.serialize());$.ajax({type: "POST",url: "http:/www.domain.com/new_site/admin/ajaxtest.php",data: "" + $.tableDnD.serialize(),success: function(html){}});}});}); </script> the alert box does give the correct values, but when I call the ajaxtest.php page nothing happens.The ajaxtest.php page has this: <?phpinclude '../includes/db.php';$images[] = $_POST['images'];$i = 0;if(!empty($images[0])){foreach($images as $value) {foreach($value as $row){$i++;if($row != 'tHeader'){$this->Image->set('id', $row);$this->Image->set('order', $i);$this->Image->save();}}}}$con = mysql_connect($host,$username,$password);if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db($database, $con);$sql= sprintf("UPDATE `images` SET `order`='$i' WHERE `id`='$row'");if (!mysql_query($sql,$con))mysql_close($con)?> I'm pretty sure the problem is with the $images[] = $_POST['images']; but of code, but I'm not sure exactly where. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 6, 2013 Share Posted February 6, 2013 Is there actually something in $_POST called "images"? You can use your browser's developer tools to verify that. Otherwise, what you're telling PHP to do is to push a new item onto the end of the array $images, but $images isn't an array yet, you haven't declared it to be anything. I'm not sure why you're trying to push an item onto the array if it's the only item though, why not just set it to $images? You can output whatever you want in that PHP script to help you debug it, and you can see the response from PHP also in your browser's developer tools to help you figure out what it's doing. Link to comment Share on other sites More sharing options...
unplugged_web Posted February 7, 2013 Author Share Posted February 7, 2013 Is there actually something in $_POST called "images"? You can use your browser's developer tools to verify that. Otherwise, what you're telling PHP to do is to push a new item onto the end of the array $images, but $images isn't an array yet, you haven't declared it to be anything. I'm not sure why you're trying to push an item onto the array if it's the only item though, why not just set it to $images? You can output whatever you want in that PHP script to help you debug it, and you can see the response from PHP also in your browser's developer tools to help you figure out what it's doing.Images is the table id, each row in the table has it's own id (which corresponds to the id in the database), I'm trying to automatically update the order after somebody has changed it. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 7, 2013 Share Posted February 7, 2013 That's fine, but that doesn't tell me what is actually in $_POST. You need to verify that first. Link to comment Share on other sites More sharing options...
unplugged_web Posted February 8, 2013 Author Share Posted February 8, 2013 (edited) That's fine, but that doesn't tell me what is actually in $_POST. You need to verify that first.Post should be the variables from each row. If I move a row then the " alert($.tableDnD.serialize());" line brings up a dialog box with the new order. This is what the dialog box shows: Result of $.tableDnD.serialise() is images[]=12.2&images[]=22.1&images[]=3.3&images[]=13.4&images[]=8.5&images[]=7.6 Edited February 8, 2013 by thehappyappy Link to comment Share on other sites More sharing options...
justsomeguy Posted February 8, 2013 Share Posted February 8, 2013 Post should be the variables from each row.It should be that? Are you going to verify what it actually is or just work on assumptions? Link to comment Share on other sites More sharing options...
unplugged_web Posted February 11, 2013 Author Share Posted February 11, 2013 (edited) It should be that? Are you going to verify what it actually is or just work on assumptions?I'm using alert($.tableDnD.serialize()); to check the results. This is the entire code I'm using for the drag and drop:<script language="Javascript">$(document).ready(function() { // Initialise the table$('#images').tableDnD({onDragClass: "drag",onDrop: function(table, row) { alert($.tableDnD.serialize()); $.ajax({type: "POST",url: "http:/www.domain.com/new_site/admin/ajaxtest.php",data: "" + $.tableDnD.serialize(),success: function(html){}});}});}); </script> Edited February 11, 2013 by thehappyappy Link to comment Share on other sites More sharing options...
thescientist Posted February 11, 2013 Share Posted February 11, 2013 (edited) I'm using to check the results.[/color] This is the entire code I'm using for the drag and drop:<script language="Javascript">$(document).ready(function() { // Initialise the table$('#images').tableDnD({onDragClass: "drag",onDrop: function(table, row) { alert($.tableDnD.serialize());$.ajax({type: "POST",url: "http:/www.domain.com/new_site/admin/ajaxtest.php",data: "" + $.tableDnD.serialize(),success: function(html){}});}});}); </script> that's not all the same as what he suggested you to check for That's fine, but that doesn't tell me what is actually in $_POST. You need to verify that first. you need to confirm the request is going over the wire and that PHP is getting it correctly. You can easily do that in the Networks tab of Chrome developer tools, for example. I would just have your PHP script the var_dump of $_POST, and then work from there. One trick I use when testing $_POST requests, is to swtich it $_GET for development purposes, then I can just reload the same URL in the browsers address bar until I get the script working then. then convert the PHP script to $_POST and then test it from the client side. Like JSG suggested first, make sure the server side is working. Your PHP code looks a little confused, so start from the beginning and work your way up. Edited February 11, 2013 by thescientist Link to comment Share on other sites More sharing options...
unplugged_web Posted February 19, 2013 Author Share Posted February 19, 2013 (edited) Just in case anybody else gets stuck with this, I changed the code to below: <?phpinclude '../includes/db.php'; $images[] = $_POST['images'];$i = 0;if(!empty($images[0])){foreach($images as $value) {foreach($value as $row){$i++;if($row != 'tHeader'){$con = mysql_connect($host,$username,$password);if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($database, $con);mysql_query("UPDATE `images` SET `order`='$i' WHERE `id`='$row'");mysql_close($con);}}}}?> and it now works perfectly. Edited February 19, 2013 by thehappyappy Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now