【发布时间】:2016-05-08 10:22:38
【问题描述】:
我有一个带有表单的页面。
此表单具有不同的“表格”,当用户浏览这些表格时,使用$.ajax 从网站调用信息,并且该表格获得动态添加的输入。
最后,我试图将此表单数据发布到 PHP 文件中。
我不会转储我的整个代码,因为它有很多规则。但这是我用来发帖的部分:
function postForm() {
...
var data = $('form.feedForm').serialize();
//console.log( data );
$.post('.../get.php?feed_add_save_feeding', data )
.fail( function() {
console.log('fail');
...
})
.success( function(data) {
console.log('success');
console.log(data);
...
})
}
在我的 PHP 中,我添加了这个,看看会发生什么:
print_r( $_POST );
查看我的控制台,我看到这些参数已发布:
dateType now
date_d 30
date_m 1
date_y 2016
time_h 19
time_m 27
time_s 42
herd_num_animals 150
herd 85
menu 26
feedtype_total_value 3639
tWeight 3639
weightCumu 3637
supps_name[29] Test voer 1
supps_price[29] 128
supps_dry_weight[29] 94
supps_weight[29] 1837
supps_name[34] Test voer 6
supps_price[34] 18
supps_dry_weight[34] 70
supps_weight[34] 1800
supps_name[30] Test voer 2
supps_price[30] 160
supps_dry_weight[30] 50
supps_weight[30] 1
user_id 1
PHP 中的输出是
Array
(
[dateType] => now
[date_d] => 30
[date_m] => 1
[date_y] => 2016
[time_h] => 19
[time_m] => 27
[time_s] => 42
[herd_num_animals] => 150
[herd] => 85
[menu] => 26
[feedtype_total_value] => 3639
[tWeight] => 3639
[weightCumu] => 3637
[supps_name] => Test voer 6
[supps_price] => 18
[supps_dry_weight] => 70
[supps_weight] => 1837
)
为什么不是所有提交的输入都通过 PHP??
(注意:由于篇幅原因,我故意省略了我的代码。如果我应该添加一些或全部,请发表评论)
编辑
这是完整的 JS 代码和渲染的 HTML(从 Firebug 复制):
【问题讨论】:
-
当你说
console时,你指的是网络标签吗? -
@JosephtheDreamer firebug 中的控制台
-
像
supps_name[29]这样发送的变量将是php $_POST ==$_POST['supps_name'][29]中的数组 -
@LinkinTED 这有点令人困惑......如果它们重复,你需要在 html 名称中使用
[]...... html 未显示 -
@DelightedD0D,它适用于 get.php 中的所有其他情况。但是,将 POST 更改为 GET,似乎确实可以解决问题。感谢大家的帮助
标签: javascript php jquery ajax post