I have a sortable list using JQueryUI and use the JQuery Each function to save the order using an AJAX request to a basic PHP file. The requests seem to be fine (Firebug console) but not every one is being saved by the PHP file, worse of all it doesn’t seem to be consistent, some id’s are more likely to fail but this seems to be related to their position in the list! It’s really strange, any ideas?
I have tried GET/POST, sync/async, made no difference.
Javascript
function SaveChanges() {
priority = 0;
$("#p_menu li").each(function() {
if($(this).attr("class") != "range") {
$.ajax({"url": "lib/product-menu-save.php", "data": "pm_id="+this.id+"&priority="+priority, "type": "GET"});
priority++;
}
});
}
PHP file
<?php
require_once '../includes/adminsession.php';
require_once '../lib/mysql.php';
$pm_id = $_GET['pm_id'];
$pm_priority = str_pad($_GET['priority'], 4, '0', STR_PAD_LEFT);
$save_stmt = $db->prepare("UPDATE products SET priority = ? WHERE id = ?");
$save_stmt->bind_param('si', $pm_priority, $pm_id);
if(!$save_stmt->execute()) echo $save_stmt->error; else echo 'SUCCESS';
The data looks fine going into the AJAX request and SUCCESS is returned for every request. There are around 60 items if that is relevant.
You could send the ids as a list in order to minimize the AJAX requests to a single one.
(since you will be sending all ids together you do not need to send the priority.. you can handle that server-side)
and (something like this, as i am not proficient in PHP)