我正在使用一個表單教程,顯示如何處理錯誤並通過json返回到頁面。我可以在chrome中看到json響應正在發送,但不會顯示在結果div中。那是空白的。在元素選項卡中的chrome中,我可以看到正在插入的信息未顯示。如果有人能告訴我爲什麼會發生這種情況,我將不勝感激。由於json數據不會顯示在結果div中
源表現爲進入
<div id="brtv-result" style="display: none; " class="success error">You must enter a service level</div>
jQuery的
$.ajax({
type: "POST",
url: "boxrtrvtemp.php",
cache: false,
data: send,
dataType: "json",
success: function(msg) {
$("#brtv-result").removeClass('error');
$("#brtv-result").removeClass('success');
$("#brtv-result").addClass(msg.status);
$("#brtv-result").html(msg.message);
},
error:function(){
$("#brtv-result").removeClass('success');
$("#brtv-result").addClass('error');
$("#brtv-result").html("There was an error submitting the form. Please try again.");
}
});
php的
<?php
//response array with status code and message
$response_array = array();
//validate the post form
//check the name field
if(empty($authorised)){
//set the response
$response_array['status'] = 'error';
$response_array['message'] = 'Name cannot be blank';
//check the email field
} elseif(empty($serivce)) {
//set the response
$response_array['status'] = 'error';
$response_array['message'] = 'You must enter a service level';
//check the message field
} elseif($department=="Choose Department") {
//set the response
$response_array['status'] = 'error';
$response_array['message'] = 'You must select a department';
//form validated. send email
} elseif($address=="Choose Address") {
//set the response
$response_array['status'] = 'error';
$response_array['message'] = 'You must select a retrieveal address';
//form validated. send email
} elseif(empty($boxnumber)) {
//set the response
$response_array['status'] = 'error';
$response_array['message'] = 'You must enter a box for retrieveal';
//form validated. send email
}else {
//set the response
$response_array['status'] = 'success';
$response_array['message'] = 'All items retrieved successfully';
}
//send the response back
echo json_encode($response_array);
?>
尼古拉·葉剛剛看到it..duh千恩萬謝 – bollo
你能告訴我,是應該是在阿賈克斯的成功或錯誤的部分回報PHP錯誤。如果status ==錯誤,那麼由於某種原因,結果將通過成功函數並導致窗體重置。謝謝 – bollo
只有在通話本身出現錯誤時,纔會觸發與「錯誤」相關的功能:例如,您所撥打的網址沒有響應。要檢查php是否返回錯誤,你應該檢查'if(msg.status ==='error')' –