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?
我有一个使用JQueryUI的可排序列表,并使用JQuery Every函数将AJAX请求保存到基本PHP文件中。请求看起来很好(Firebug控制台),但不是每个人都被PHP文件保存,更糟糕的是它似乎不一致,有些id更可能失败但这似乎与他们的位置有关在列表中!这真的很奇怪,任何想法?
I have tried GET/POST, sync/async, made no difference.
我尝试过GET / POST,同步/异步,没有任何区别。
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.
数据看起来很好,进入AJAX请求,并为每个请求返回SUCCESS。如果相关,大约有60个项目。
1 个解决方案
#1
0
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)
您可以将ID作为列表发送,以便将AJAX请求最小化到单个请求。 (因为您将所有ID一起发送,您不需要发送优先级..您可以处理该服务器端)
function SaveChanges() {
var id_list = [];
$("#p_menu li").each(function() {
if($(this).attr("class") != "range") {
id_list.push( this.id );
}
});
$.ajax({
url : 'lib/product-menu-save.php',
data : {ids: id_list},
type : 'POST'
});
}
and (something like this, as i am not proficient in PHP)
和(像这样,因为我不精通PHP)
$save_stmt = $db->prepare("UPDATE products SET priority = ? WHERE id = ?");
$pm_priority_value = 0;
foreach ($_POST['ids'] as $pm_id) {
$pm_priority = str_pad($pm_priority_value, 4, '0', STR_PAD_LEFT);
$save_stmt->bind_param('si', $pm_priority, $pm_id);
if(!$save_stmt->execute()) echo $save_stmt->error; else echo 'SUCCESS';
$pm_priority_value++;
}
#1
0
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)
您可以将ID作为列表发送,以便将AJAX请求最小化到单个请求。 (因为您将所有ID一起发送,您不需要发送优先级..您可以处理该服务器端)
function SaveChanges() {
var id_list = [];
$("#p_menu li").each(function() {
if($(this).attr("class") != "range") {
id_list.push( this.id );
}
});
$.ajax({
url : 'lib/product-menu-save.php',
data : {ids: id_list},
type : 'POST'
});
}
and (something like this, as i am not proficient in PHP)
和(像这样,因为我不精通PHP)
$save_stmt = $db->prepare("UPDATE products SET priority = ? WHERE id = ?");
$pm_priority_value = 0;
foreach ($_POST['ids'] as $pm_id) {
$pm_priority = str_pad($pm_priority_value, 4, '0', STR_PAD_LEFT);
$save_stmt->bind_param('si', $pm_priority, $pm_id);
if(!$save_stmt->execute()) echo $save_stmt->error; else echo 'SUCCESS';
$pm_priority_value++;
}