2009-07-17 103 views
1

當我做一個AJAX請求我去一個PHP腳本,爲我做一些事情。 它可以從db中刪除一條記錄,或者它不會。jQuery的AJAX - PHP的問題

如果沒問題。我想刪除並更新我的HTML表格。否則,我想顯示一條錯誤消息。

但在我的PHP,我不能這樣做

return false; 
return true; 

我需要我的呼應結果或者我的消息,然後我的jQuery腳本會用它做什麼。

但我怎麼能讓jquery根據結果做些什麼?

例如這裏:

jQuery.ajax({ 
      type: "POST", 
      data: "id=" +id, 
      url: "ajax_handler.php", 
      success: function(msg){ 
       jQuery(tr).remove(); 
       jQuery("div.result").append("<p>"+msg+"</p>"); 
       jQuery("div.result").show(); 
       jQuery("div.result").fadeOut(5000); 

      }, 
      error: function(msg){ 

      } 

和PHP代碼

$id = C_GPC::getPOSTvar("id"); 
$result =""; 
if(isset($id) and ($id > 0)){ 
    $p = new C_product(); 
    $deleted = $p->deleteCategorie($id); 
    if($deleted){ 
     $result = "ok"; 
    }else{ 
     $result = "not ok"; 
    } 
}else{ 

    $result = "not ok"; 
} 
echo $result; 

我想做一個好時和B時,也不行。我應該如何處理這個問題? });

回答

1

msg(成功結果參數)將返回服務器/ PHP腳本的任何輸出。所以對於你的PHP腳本,如果它不刪除記錄,那麼你可以回顯出錯信息。在你的Javascript代碼中,你可以檢查:

if(msg == 'not ok') 
{ 
    alert('error occured'); 
} 

if(msg == 'ok') 
    alert('update the table'); 
} 

你可以爲你的表/更新設置類似的東西。

0

你可以使用jQuery的get功能:

看一看底部的例子(例如標籤下)。您可以處理請求的結果並決定如何處理它。