gpt4 book ai didi

javascript - jQuery $.post() 未向 php 发送正确的信息

转载 作者:行者123 更新时间:2023-11-29 23:27:56 26 4
gpt4 key购买 nike

我刚刚开始学习 jQuery、php 等。我有一个 div(比例),其中包含具有两个属性的图像。我想将这些属性写入数据库。因为它们可以更改(拖放),所以我想将所有 img 属性发送到数据库,以便每次将新项目放入 div 时进行更新。但不是发送 div 的每个子项,而是仅发送当前(新添加的)项目(多次)。

jQuery

stop: function(event, ui) {
$('#scale').children().each(function(){
var idd = $(this).attr("data-id");
var size2 = parseInt($("#scale > img").length);
var pos = parseInt($(this).index());
var rat = 1.00-(1/size2*pos);
$(this).attr('newRating', rat);

console.log("We send Movie with ID : " + idd + " on Position: " +pos + " to the DB");

$.post("userInfo.php",
{
id:idd,
rating:rat

},
function(data, status){
console.log("Data: " + data + "\nStatus: " + status + " \nof " + $(ui.item).attr('data-id') + " \nwith " + $(ui.item).attr('newRating') );
}
);

});
}

第一个 console.log 为我提供了正确的 ID 和评级,但我从 .post() 调用返回的数据是错误的。

php

        include_once('db.php');

$id = $_POST['id'];
$rating = $_POST['rating'];


if (mysql_query("INSERT INTO MovieRating SET id = '$id', rating ='$rating' ")){
echo "Successfully Inserted";
}
else{
echo "Insertion Failed";
}

最佳答案

console.log("Data: " + data  + "\nStatus: " + status + " \nof " +  $(ui.item).attr('data-id') + " \nwith " + $(ui.item).attr('newRating') );
^^^^^^^ ^^^^^^

每次登录控制台时,您都会读取 ui.item。您想要当前迭代中的项目。

console.log("Data: " + data  + "\nStatus: " + status + " \nof " +  idd + " \nwith " + rat );

关于javascript - jQuery $.post() 未向 php 发送正确的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26807695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com