我正撞在墙上,试图找出为什么我一直得到'TypeError:'msgs'是空错误'。我一遍又一遍地检查了我的jQuery和php代码,甚至尝试了console.log(msgs),但是在控制台中没有显示任何内容。有人可以帮我看看我在哪里滑倒。非常感谢
// Function to enter intake user area
$(function ()
{
$.validator.setDefaults(
{
errorClass: 'form_error',
errorElement: 'span'
});
$("#USRboxint").validate(
{
rules:
{
requested:
{
required: true
},
service:
{
required: true
},
dept:
{
required: true
},
address1:
{
required: true
},
datepicker:
{
required: true
},
box_add:
{
required: true
}
},
messages:
{
requested:
{
required: '* required: You must enter your name'
},
dept:
{
required: "* required: You must select a department"
},
address1:
{
required: "* required: You must select a customer address"
},
service:
{
required: "* required: You must select a tier level"
},
box_add:
{
required: "* required: You must enter a number"
},
datepicker:
{
required: "* required: You must enter a date"
}
},
submitHandler: function() {
if ($("#USRboxint").valid() === true) {
var data = $("#USRboxint").serialize();
$.post('/domain/users/boxesadd.php', data, function(msgs) {
var messageOutputs = '';
for (var i = 0; i<msgs.length; i++){ <--- **ERROR IS HERE**
messageOutputs += msgs[i].box+' ';
console.log(messageOutputs);
}
$("#USRaddbox").html("You have entered box: " + "<b>" + messageOutputs + "</b><br /> You may now close this window.");
$("#USRboxint").get(0).reset();
}, 'json');
} else
{
return;
}
},
success: function(msg) {
//$("#BA_addbox").html("You have entered a new entry");
//$("#BA_boxform").get(0).reset();
}
});
});
PHP后端
<?php
$requested = mysql_real_escape_string($_POST['requested']);
$activity = mysql_real_escape_string($_POST['activity']);
$address = mysql_real_escape_string($_POST['address1']);
$service = mysql_real_escape_string($_POST['service']);
$box = mysql_real_escape_string($_POST['box_add']);
//$destroydate = mysql_real_escape_string($_POST['BA_destdate']);
$date = DateTime::createFromFormat('d/m/Y', $_POST['datepicker']);
$destdate = $date->format('Y-m-d');
$authorised = mysql_real_escape_string($_SESSION['kt_name_usr']);
$submit = mysql_real_escape_string($_POST['submit']);
$dept = mysql_real_escape_string($_POST['dept']);
$array = split('[,]', $_POST['box_add']);
if (isset($_POST['submit'])) {
$form = array();
foreach ($array as $box) {
$form[] = array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destroydate,
'authorised'=>$authorised,
'submit'=>$submit);
}
}
$result=json_encode($form);
echo $result;
?>
答案 0 :(得分:2)
.serialize()
不会在发送到服务器的数据中包含submit
按钮。只有在正常提交表单时才会自动发送。
尝试:
var data = $("#USRboxint").serialize() + '&submit=true';