[英]Why is this array not being passed?
Simple script sending form data to a PHP script to be processed. 将表单数据发送到PHP脚本进行处理的简单脚本。 The data in in an assoc array and output to the console shows it is present.
assoc数组中的数据并输出到控制台,表明存在该数据。 Data is passed using a jquery AJAX function, however the PHP script is not receiving array.
使用jquery AJAX函数传递数据,但是PHP脚本未接收到数组。 However, if I hardcode the data into the function, the data is passed.
但是,如果我将数据硬编码到函数中,则会传递数据。
If have tried setting the AJAX function 'method' to POST and also removing that option. 如果尝试将AJAX函数“方法”设置为POST并删除该选项。 I have tried different formats for the array.
我已经尝试了数组的不同格式。 Passing the array as part of a hard coded array.
将数组作为硬编码数组的一部分传递。
Javascript side Javascript方面
function saveInformation(e) {
var d = []
var f = $(e)[0].id; //Form name
var formData = $('#'+f).serializeArray();
formData.forEach(function(item){
d[item.name] = item.value;
})
console.log('data',d)
$.ajax({
data: d,
url: "TableUpdate.php"
})
.done(function(e) { $('#agentGeneralInfoTitle').text('Agent General Information (Saved)'); console.log(e);})
.fail(function(e) {
alert("save failed\n" + e.responseText); console.log(e);
});
}
The console.log command shows data in variable d. console.log命令显示变量d中的数据。 All the data is present and properly formatted.
所有数据均已存在并已正确格式化。
PHP side PHP方面
<?
require_once('/var/www/Debugging/ChromePhp.php');
chromePHP::log($_REQUEST,$_POST);
:
:
:
?>
this prints out "[],[]". 这将打印出“ [],[]”。 It complete correctly with a determination that the data is not present.
可以正确确定是否不存在数据。
I expect the data to be passed and the chromePHP::log to show an array with data. 我希望传递数据,并且chromePHP :: log显示带有数据的数组。
var d
should be an object not array. var d
应该是对象而不是数组。
Arrays have numeric indices only in javascript 数组仅在javascript中具有数字索引
Change 更改
var d = []
To 至
var d = {}
Also using serialize()
instead of serializeArray()
would make it simpler as you wouldn't need to loop through and create d
at all 另外,使用
serialize()
而不是serializeArray()
会使其更简单,因为您根本不需要遍历和创建d
function saveInformation(e) {
// assuming `e` is form element
var formData = $(e).serialize();
$.ajax({
data: formData ,
url: "TableUpdate.php"
})
.done(function(e) { $('#agentGeneralInfoTitle').text('Agent General Information (Saved)'); console.log(e);})
.fail(function(e) {
alert("save failed\n" + e.responseText); console.log(e);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.