我正在使用jQuery將數據輸入到php文件並通過json返回結果。但是,json出現在螢火蟲中,但變量'messageOutput'不在窗體上顯示結果。如果我將'messageOutput'替換爲'msg.box',那麼它會打印正常。jQuery submitHandler:函數不顯示數據
如果我在窗體上輸入了多於一個項目,那麼它會出錯:'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();
}
boxesadd.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;
?>
在哪裏我會把console.log語句?我已經試過這樣但沒有顯示控制檯:var messageOutput =''; (var i = 0; i
user1532468
把它放在循環之外,正上方。如果它在循環中,並且它不會觸發,這表明msg不是數組 –