【发布时间】:2016-10-03 18:14:48
【问题描述】:
我有一个这个变量:
var order = $('#listadoImages').sortable('serialize');
当将订单变量从 JSON 发送到 PHP 时,显示“id[]=18&id[]=19”,我的问题是我无法在 php 中读取它。我尝试使用 foreach 但返回错误。
我的 ajax 代码:
$("#mainAction").click(function(){
//alert("New position: " + ui.item.index());
var order = $('#listadoImages').sortable('serialize');
var rutas = [];
$('#listadoImages li').each(function(index, li){
var bg = $(this).find('span').css('background-image');
bg = bg.replace('url(','').replace(')','').replace(/\"/gi, "");
var res = bg.substring(42);
if(index == 0){
rutas += res;
}else{
rutas += ";" + res;
}
});
//alert(rutas);
$.ajax({
type: "POST",
url: "functions/guardar.php",
data: {
orden : order,
path : rutas
},
"success":function(data){
}
});
});
我的 PHP 代码:
$paths = $_POST['path'];
$orden = $_POST['orden'];
$rutas = explode(";", $paths);
if(isset($paths)){
$sql = "SELECT * FROM imagenes";
$respuesta = mysql_query($sql);
if($respuesta){
$i=0;
while( $rs = mysql_fetch_assoc($respuesta) ){
$rutaBD = $rs['ruta_imagen'];
foreach ($orden as $k => $value) {
foreach ($rutas as $key => $rut) {
for($i=0;$i<count($rut);$i++){
if($rutaBD == $rut){
//Here Update Statment
}
}
}
}
}
}
感谢您的自述文件。我希望有一个人可以帮助我。 谢谢! 问候。
【问题讨论】:
-
我没有看到您尝试在 PHP 的任何地方使用
$orden? (它应该包含一个数组['id => [first, second, third, ...etc...]]。转储 `$_POST? 变量并查看它包含什么。 -
嗨@Magnus Eriksson,JSON 返回“id[]=18&id[]=19”并且无法从 php 读取
-
JSON 是一种格式,不能“返回”任何东西。你说的是 Javascript 吗?
-
@MagnusEriksson 我的问题在于 $.ajax 调用中的“orden:order”,这显示了 php $_POST['orden'] 中的 id[]=18&id[]=19。我需要更新语句的值 id。了解我吗?