我正在使用 jquery 使用序列化提交表单,一切都很好,除非我输入超过 1 个框。如果我输入 1 个框,则 msg.box 出现在 #BA_addbox 中。但是,如果我使用 , 作为分隔符输入超过 1 个框,则不会显示 msg,并且 firebug 中不会出现 json 选项卡。只是正确的html。我的代码哪里出错了。
我创建了一个数组并使用 foreach 和explode 来分隔值,但没有返回多个值。谢谢
更新:在 php 脚本中正在收集 vars,如下所示:
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();
}