我正在使用一个表单教程,该教程展示了如何处理错误并通过 json 返回页面。我可以在 chrome 中看到 json 响应正在发送,但没有显示在结果 div 中。那是空白的。在 Chrome 的元素选项卡中,我可以看到信息正在插入但未显示。如果有人能告诉我为什么会发生这种情况,我将不胜感激。谢谢
源显示为输入的内容
<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);
?>
最佳答案
那是因为你必须显示我认为的 div,因为它一开始是隐藏的:
$.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);
$("#brtv-result").show()//show it
},
error:function(){
$("#brtv-result").removeClass('success');
$("#brtv-result").addClass('error');
$("#brtv-result").html("There was an error submitting the form. Please try again.");
$("#brtv-result").show()//show it
}
});
关于php - json 数据未显示在结果 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7415259/