目前,我正在使用一个脚本,它以 msg[0].box msg[1].box 等格式从 json 返回一个值。如果用户输入说 2 个项目,那没关系。但是,如果用户输入的内容不止这些怎么办。我的一个朋友建议我查看 jquery 每个函数以重新调整多个值,但鉴于我对 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();
}