我正在使用 jQuery 将数据输入到 php 文件并通过 json 返回结果。但是,json 出现在 firebug 中,但变量“messageOutput”未在表单上显示结果。如果我用 'msg.box' 替换 'messageOutput' 那么它打印得很好。
如果我在表单上输入超过 1 个项目,则会出错:'object object'。
有人可以指出我的错误在哪里,因为我多年来一直在努力解决这个问题。如果您需要查看更多代码,请询问。非常感谢。
jQuery代码:
submitHandler: function() {
if ($("#BA_boxform").valid() === true) {
var data = $("#BA_boxform").serialize();
$.post('/domain/admin/requests/boxes/boxesadd.php', data, function(msg) {
var messageOutput = '';
for (var i = 0; i<msg.length; i++){
messageOutput += msg[i].box+' ';
}
$("#BA_addbox").html("You have entered box: " + "<b>" + messageOutput + "</b><br /> You may now close this window.");
$("#BA_boxform").get(0).reset();
}, 'json');
} else
{
return;
}
},
success: function(msg) {
//$("#BA_addbox").html("You have entered a box");
//$("#BA_boxform").get(0).reset();
}
boxadd.php
<?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']);
$destroydate = mysql_real_escape_string($_POST['BA_destdate']);
$authorised = mysql_real_escape_string($_POST['BA_authorised']);
$submit = mysql_real_escape_string($_POST['submit']);
$boxerrortext = 'You must enter a box for intake';
$array = split('[,]', $_POST['BA_box']);
if (isset($_POST['submit'])) {
foreach ($array as $box) {
if (empty($box)) {
$error = array('boxerrortext'=>$boxerrortext);
$output = json_encode($error);
echo $output;
}
else
{
$form=array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destroydate,
'authorised'=>$authorised,
'submit'=>$submit);
$result=json_encode($form);
echo $result;
?>