我有一个数组,我通过 AJAX 将它传递给 PHP,很好 - 从 PHP 文件获得正确的响应。然而,我现在希望在 AJAX 的数据对象中包含一个“id”,以便与原始数组一起发送到 PHP 文件。我似乎无法弄清楚这一点。如果有人提供任何意见,我将不胜感激。
var id = $("input[name='id']").val();
var posArr = ['1', '2', '3', '4', '5', '6', '7'];
$.ajax({
type: 'POST',
url: 'php/file.php',
data: { positions: posArr },
dataType: 'json',
cache: false,
success: function(output) {
console.log('success -> ' + output);
},
error: function(output) {
console.log('fail -> ' + output);
}
});
我的目标是使用传递的 id 根据该 id 搜索数据库,然后使用数组 (posArr) 中的项目存储/更新表。
谢谢。
最佳答案
I am now looking to include an "id" in the AJAX's data object
然后将其包含在发送到服务器的对象中:
data: { positions: posArr, id: id }
服务器端它将像 positions
数据元素一样被读取。在这种情况下,只需:
$_POST['id']
关于php - 将 Ajax 数据中的第二个值传递给 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44635274/