2012-09-03 55 views
0

我一直在尋找答案,但仍然不知如何去做。說實話,我不擅長編程,我仍然處於學習的過程中。下面從另一個PHP文件獲取POST方法的錯誤

是我的代碼:

$.ajax({ 
type: "POST", 
url: "process.php", 
data: { 
postid: post_id 
}, 
success: function(){ 
document.getElementById('processed').innerHTML = post_id; 
... 

我沒有用的問題,它只是我想要得到process.php錯誤(如果有任何)。

process.php代碼,我想要得到的錯誤:

if($result){ 
     while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
      $m = $row['processedID']; 
      $id = trim($_POST ['postid']); 
     try { 
      ..something... 
     } 
     catch (APIHandle $e) { 
      $output .= "<p>'". $row['name'] . "' processing failed</p>"; 
     } 
} 
} 

我想這條線:

$output .= "<p>'". $row['name'] . "' processing failed</p>"; 

,並顯示在我的第一個PHP文件作爲錯誤和輸出它作爲一個文本。例如,用戶點擊一個按鈕,它檢查輸入並輸出錯誤,如果有任何使用這些代碼。

謝謝你的幫助!

編輯! 我現在使用的是由andy提供的代碼,因爲它處理錯誤,它給了我一個選項,以便在發生錯誤時做什麼。問題是我不知道如何使用textfields /表單項來處理它。 這裏是我更新的代碼:

var textfieldvalue = $("#text").val(); 
    var post_id = textfieldvalue; 
    $.ajax({ 
      type: "POST", 
      url: "process.php", 
      dataType: "json", 
      data: { 
       postid: post_id 
      }, 
      success: function(result){ 
       if (result.error) { 
        $('#accesserror').show(); 
        $('#error').html(result.error).show(); 
       } 
       else { 
        $("#loading").hide(); 
        $("#processor").show(); 

       } 

我知道,有什麼錯。請問 ?這是我最後一次請求幫助。 謝謝你,所有的答案將非常感謝!

+0

您想在ajax調用返回時獲取消息嗎? – Wearybands

+0

你是否檢查過你是否在process.php上獲得$ _POST ['postid']? –

+0

@UmairIqbal,是的。這就是我想要做的 –

回答

0

然後,您應該返回$output,其中包含錯誤json_encoded(),因爲您可能正在使用正常結果。你也可以用一些錯誤信息編碼一個數組。

+0

我想我應該學習更多,提供的事實,我不知道什麼是「json_encoded()」。 –

0
$.ajax({ 
    type: "POST", 
    url: "process.php", 
    data: { 
    postid: post_id 
    }, 
    success: function(data){ 
    document.getElementById('processed').innerHTML = data; 
    } 

你process.php

if($result){ 
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
     $m = $row['processedID']; 
     $id = trim($_POST ['postid']); 
    try { 
     ..something... 
    } 
    catch (APIHandle $e) { 
     echo $output .= "<p>'". $row['name'] . "' processing failed</p>"; 
    } 
    } 

如果你的代碼放在process.php抓住它wqill返回輸出和阿賈克斯dispay該輸出

+0

thnk你!我會嘗試知道:) –

+0

讓我知道它是否工作...如果不是,我可以爲您提供另一種解決方案 – Wearybands

+0

@UmairIqbal不應該是'回聲'

'「。 $ row ['name']。 「'處理失敗

」;'? – Andy0708

0

這聽起來像你想要的是JSON。然後,您可以檢查error字段是否爲空,如果不是,則顯示錯誤。嘗試下面的內容。

if($result) { 
    $result = array(); 

    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
     $m = $row['processedID']; 
     $id = trim($_POST ['postid']); 

     try { 
      // something 
     } 
     catch (APIHandle $e) { 
      $result['error'] = 'Processing failed!'; 
     } 
    } 

    echo json_encode($result); 
} 

而jQuery的:

$.ajax({ 
    type: "POST", 
    url: "process.php", 
    dataType: "json", 
    data: { 
     postid: post_id 
    }, 

    success: function(result){ 
     // There was an error 
     if (result.error) { 
      $('#error').html(result.error).show(); 
     } 

     // No error 
     else { 
      // Something 
     } 
    } 
}); 

然後,你可以簡單地在$result['something_else']返回其他數據,並表明如果沒有錯誤。此外,該解決方案還爲您提供了很大的靈活性,可以選擇想要顯示數據的位置。例如,你可能想要顯示你的錯誤,而不是你從PHP返回的其他數據。如果你只是用PHP回顯數據,你不得不在同一個地方展示它,然後你必須依靠CSS來改變定位。

希望它有幫助。

+0

感謝您的幫助!天哪,很多好的答案瞬間! –

+0

如果出現多個錯誤,該怎麼辦? –

+0

@NishaLeine然後你可以從PHP返回一個錯誤數組,並通過它們在jQuery中循環。我昨天寫了一個[回答另一個問題](http://stackoverflow.com/questions/12237986/load-more-getting-10-more-results/12238038#12238038),展示瞭如何做到這一點。或者,一個不太美觀但更簡單的解決方案是將所有錯誤都放入錯誤字符串中,如下所示:'$ result ['error']。='另一個錯誤';' – Andy0708

相關問題