gpt4 book ai didi

php - 从 json 返回多个值

转载 作者:行者123 更新时间:2023-11-28 13:41:34 25 4
gpt4 key购买 nike

目前,我正在使用一个脚本,该脚本以 msg[0].box msg[1].box 等格式从 json 返回一个值。如果用户输入 2 个项目,那就可以了。但如果用户输入的内容不止于此,该怎么办?我的一个 friend 建议我查看 jquery every 函数来重新调整多个值,但由于我对 jquery 相当陌生,如果有人可以帮助我使用我的代码实现此目的,我将不胜感激。

我也很困惑为什么返回值时firebug中没有json选项卡,只有reposnse和html。这是正常的吗?请注意,输入值时使用 , 作为分隔符。

php代码

    $dept = mysql_real_escape_string($_POST['customerdept']);
$company = mysql_real_escape_string($_POST['BA_customer']);
$address = mysql_real_escape_string($_POST['customeraddress']);
$service = mysql_real_escape_string($_POST['BA_service']);
$box = mysql_real_escape_string($_POST['BA_box']);
$date = DateTime::createFromFormat('d/m/Y', $_POST['BA_destdate']);
$destdate = $date -> format('Y-m-d');
$authorised = mysql_real_escape_string($_POST['BA_authorised']);
$activity = 'New Intake';
$submit = mysql_real_escape_string($_POST['submit']);
$boxerrortext = 'You must enter a box for intake';

$array = explode(',', $_POST['BA_box']);

if (isset($_POST['submit'])) {
foreach ($array as $box) {


$form=array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destdate,
'authorised'=>$authorised,
'submit'=>$submit);

$result[]=$form;

}

echo json_encode( $result );
}

jquery代码

submitHandler: function()   {
if ($("#BA_boxform").valid() === true) {
var data = $("#BA_boxform").serialize();
$.post('/sample/admin/requests/boxes/boxesadd.php', data, function(msg) {
$("#BA_addbox").html("You have entered box(es): " + "<b>" + msg[0].box + " " + msg[1].box + "</b><br /> You may now close this window.");
//console.log(msg[0].box);
$("#BA_boxform").get(0).reset();
}, 'json');

} else

{
return;
}
},
success: function(msg) {
//$("#BA_addbox").html("You have entered a box");
//$("#BA_boxform").get(0).reset();
}

最佳答案

关于如何使用$.each函数...

var html = "You have entered box(es): <b>"
$.each(msg, function(index, element) {
if(index > 0)
html += " ";
html += element.box;
});
html += "</b><br /> You may now close this window."

关于php - 从 json 返回多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17523138/

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