我正在使用jQuery通过序列化提交表单,一切都很好,除非我输入超过1个框。如果我只输入1个框,则msg.box会出现在#BA_addbox中。但是,如果我使用逗号作为分隔符输入多个框,则不会显示任何消息,并且在Firebug中不会出现JSON选项卡,只有正确的HTML。我的代码哪里出了问题?
我已经创建了一个数组,并使用foreach和explode来分离值,但没有返回多个值。谢谢。
更新:变量在php脚本中被收集,如下所示:
我已经创建了一个数组,并使用foreach和explode来分离值,但没有返回多个值。谢谢。
更新:变量在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']);
$submit = mysql_real_escape_string($_POST['submit']);
$array = explode(",", $_POST['BA_box']);
if (isset($_POST['submit'])) {
foreach ($array as $box) {
//$sql = "INSERT INTO `act` (service, activity, company, address, department, user, destroydate, date, item, new) VALUES ('$service', '$activity', '$company', '$address', '$dept', '$authorised', '$destdate', NOW(), '$box', 1)";
//$result = runSQL($sql) or die(mysql_error());
$form=array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destdate,
'authorised'=>$authorised,
'submit'=>$submit);
$result=json_encode($form);
echo $result;
}
}
jQuery代码
submitHandler: function() {
if ($("#BA_boxform").valid() === true) {
var data = $("#BA_boxform").serialize();
$.post('/domain/admin/requests/boxes/boxesadd.php', data, function(msg) {
$("#BA_addbox").html("You have entered box(es): " + "<b>" + msg.box + "</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();
}
json_encode()
函数。 - nnnnnnecho $result;
之前添加代码header('Content-Type: application/json');
。 - softsdev