我正在尝试使用成功发生的ajax将表单数据发送到php,form1包含表格我的问题是当我通过ajax序列化到php之后发送表单数据时我打印它
php方面它给了我'd'
字符
我不知道为什么会发生这种情况
jQuery('.finialize').click(function(){
var dte={};
var $form = $("#form1");
dte = getFormData($form);
dte['info']='disrepency';
console.log(dte);
senddata(dte,"../controller/register.php");
});
function getFormData($form){
var unindexed_array = $form.serializeArray();
var indexed_array = {};
$.map(unindexed_array, function(n, i){
indexed_array[n['name']] = n['value'];
});
return indexed_array;
}
function senddata(data,url){
jQuery.ajax({
type: "POST",
url: url,
data: {'data':data},
dataType: "json",
})
.done(function(result){
if(result['errCode'].hasOwnProperty('-1')){
alert(result['errMsg']);
}
else{
alert(result['errMsg']);
}
console.log(result);
})
.fail(function( jqXHR, textStatus) {
console.log( "failed due:"+ textStatus);
})
}
PHP端
foreach($dt as $key=>$val){
echo $val['InvoiceNo'].":";
echo $val['ContractID']."\n";
}
我输出为
148523:1/14S/0
1485231:1/14S/0
1485232:1/14S/0
1485233:1/14S/0
d:d
为什么我得到d:d
这是我从ajax获得的php数据
Array
(
[input_[1] => Array
(
[InvoiceNo] => 148523
[ContractID] => 1/14S/0
[Amount] =>
[SetOFF] =>
[Comment] =>
[childInvoice_1] => Array
(
[1] => Array
(
[InvoiceNo] => 148523
[ContractID] => 1/14S/0
[Amount] =>
[SetOFF] =>
[Comment] =>
[Line_item] => 144
)
[2] => Array
(
[InvoiceNo] => 148523
[ContractID] => 1/14S/0
[Amount] =>
[SetOFF] =>
[Comment] =>
[Line_item] => 149
)
)
)
[input_[2] => Array
(
[InvoiceNo] => 1485231
[ContractID] => 1/14S/0
[Amount] =>
[SetOFF] =>
[Comment] =>
)
[input_[3] => Array
(
[InvoiceNo] => 1485232
[ContractID] => 1/14S/0
[Amount] =>
[SetOFF] =>
[Comment] =>
[childInvoice_3] => Array
(
[1] => Array
(
[InvoiceNo] => 1485232
[ContractID] => 1/14S/0
[Amount] =>
[SetOFF] =>
[Comment] =>
[Line_item] => 146
)
)
)
[info] => disrepency
)
答案 0 :(得分:1)
请改变你的foreach,如下所示,并测试一次。: -
foreach($dt as $key=>$val){
if($key != 'info'){
echo $val['InvoiceNo'].":";
echo $val['ContractID']."\n";
}
}