2011-09-14 32 views
1

我正在使用一個表單教程,顯示如何處理錯誤並通過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); 

?> 

回答

3

那是因爲你必須拿出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 
    } 
    }); 
+0

尼古拉·葉剛剛看到it..duh千恩萬謝 – bollo

+0

你能告訴我,是應該是在阿賈克斯的成功或錯誤的部分回報PHP錯誤。如果status ==錯誤,那麼由於某種原因,結果將通過成功函數並導致窗體重置。謝謝 – bollo

+0

只有在通話本身出現錯誤時,纔會觸發與「錯誤」相關的功能:例如,您所撥打的網址沒有響應。要檢查php是否返回錯誤,你應該檢查'if(msg.status ==='error')' –

1

你的DIV設置爲style="display: none; "

+0

什麼時候頁面加載時HTML元素已經顯示?我可以在「錯誤」功能中更改HTML元素中顯示的文本。 – Kingsolmn

+0

@Kingsolmn:對不起。你能澄清你在說什麼嗎? – Herbert

+0

我在尋找的是:我有一個顯示錶格的頁面,並且我可以通過添加單個​​元素或使用jQuery更改該元素的內容來修改元素。我遇到的問題是添加更多​​元素。 – Kingsolmn