gpt4 book ai didi

jquery - 无法将数组作为表单参数发送

转载 作者:行者123 更新时间:2023-12-01 07:04:12 24 4
gpt4 key购买 nike

我创建了一个隐藏表单,并尝试在 ajax 成功时添加一些其他输入字段,然后尝试提交此表单。但是,将数组附加到字段中并不正确。

success : function(resp) {
$('#checkout').append('<input type="text" name="subtotal" value="'+resp.data.subTotal+'" />');
$('#checkout').append('<input type="text" name="tax" value="'+resp.data.tax+'" />');
$('#checkout').append('<input type="text" name="times" value="'+resp.data.times+'" />');
}

这样做,输入字段时间变得像

<input type="text" name="times" value="[{"start_date":"2019-06-10","end_date":"2019-06-10","start_time":"08:00:00","end_time":"09:00:00"},{"start_date":"2019-06-11","end_date":"2019-06-11","start_time":"08:00:00","end_time":"09:00:00"},{"start_date":"2019-06-12","end_date":"2019-06-12","start_time":"08:00:00","end_time":"09:00:00"}]"

响应看起来像

{"status":"200","data":{"times":[{"start_date":"2019-06-10","end_date":"2019-06-10","start_time":"08:00:00","end_time":"09:00:00"},{"start_date":"2019-06-11","end_date":"2019-06-11","start_time":"08:00:00","end_time":"09:00:00"},{"start_date":"2019-06-12","end_date":"2019-06-12","start_time":"08:00:00","end_time":"09:00:00"}],"subTotal":300,"tax":24}}

当我提交此表单时,它没有发送时间值。

print_r($_POST)

Array
(
[_token] => x8THCiWyxRXnb9T3mKI3ae8XzfKwHicW1SbwBGXS
[subtotal] => 300
[tax] => 24
[times] => [{
)

我想以这种或那种方式将这些时间值发送到服务器。

最佳答案

在成功响应中对 JSON 数据进行字符串化:

 success : function(resp) {
let times=JSON.stringify(resp.data.times);

$('#checkout').append('<input type="text" name="times" value="'+times+'" />');

}

并在服务器端解码:

 $posted_data = json_decode($_POST['times'], true);

关于jquery - 无法将数组作为表单参数发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56471769/

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