Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 结果div中未显示json数据_Php_Jquery_Json - Fatal编程技术网

Php 结果div中未显示json数据

Php 结果div中未显示json数据,php,jquery,json,Php,Jquery,Json,我正在使用一个表单教程,演示如何处理错误并通过json返回页面。我可以在chrome中看到json响应正在发送,但它没有显示在结果div中。结果div为空。在“元素”选项卡的chrome中,我可以看到正在插入的信息只是没有显示。如果有人能告诉我为什么会这样,我将不胜感激。谢谢 输入时显示的源 <div id="brtv-result" style="display: none; " class="success error">You must enter a service leve

我正在使用一个表单教程,演示如何处理错误并通过json返回页面。我可以在chrome中看到json响应正在发送,但它没有显示在结果div中。结果div为空。在“元素”选项卡的chrome中,我可以看到正在插入的信息只是没有显示。如果有人能告诉我为什么会这样,我将不胜感激。谢谢

输入时显示的源

<div id="brtv-result" style="display: none; " class="success error">You must enter a service level</div>
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
     }
   });

那是因为你必须显示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
     }
   });

您的div设置为
style=“display:none;”“

您的div设置为
style=“display:none;”“

您能告诉我,php中的错误应该在ajax的成功或错误部分返回吗。如果status==error,那么由于某种原因,结果将通过success函数传递,并导致表单重置。感谢只有在调用本身出现错误时,才会触发您绑定到“error”的函数:例如,您正在调用的url没有响应。要检查php是否返回错误,您应该检查
if(msg.status==“error”)
您能告诉我,php中的错误应该在ajax的成功或错误部分返回吗。如果status==error,那么由于某种原因,结果将通过success函数传递,并导致表单重置。感谢只有在调用本身出现错误时,才会触发您绑定到“error”的函数:例如,您正在调用的url没有响应。要检查php是否返回错误,您应该检查
if(msg.status==“error”)
当加载页面时HTML元素已经显示时该怎么办?我可以在“error”函数中更改HTML元素中显示的文本。@Kingsolmn:对不起。你能澄清一下你在说什么吗?我要找的是:我有一个显示有表的页面,我可以通过添加单个元素或使用jQuery更改元素的内容来修改元素。我遇到的问题是添加了更多的元素。听起来你有一个完全不同的问题。可能是相关的。另请参见:。如果这没有帮助,请考虑一个问题,我们可以检查你的代码。感谢这些链接,因为某些原因,我无法让我的大脑用JavaScript思考;我想我太习惯OOP了。当加载页面时HTML元素已经显示时会怎么样?我可以在“error”函数中更改HTML元素中显示的文本。@Kingsolmn:对不起。你能澄清一下你在说什么吗?我要找的是:我有一个显示有表的页面,我可以通过添加单个元素或使用jQuery更改元素的内容来修改元素。我遇到的问题是添加了更多的元素。听起来你有一个完全不同的问题。可能是相关的。另请参见:。如果这没有帮助,请考虑一个问题,我们可以检查你的代码。感谢这些链接,因为某些原因,我无法让我的大脑用JavaScript思考;我想我太习惯了。