2013-01-19 55 views
0

我正在使用此代碼刪除一行..我的代碼工作正常我正在使用數據表,其中每行都有一個按鈕在他們面前刪除..我想要的是什麼時候確認對話框出現,用戶按下確定,然後它刪除行,並顯示在datable剩餘的行,而不刷新..我的意思是當時如果我按下確定按鈕它成功刪除行..但刪除行仍然在那裏該表,直到我刷新頁面,然後它從表中刪除..是可能的?..因爲我不想從用戶,他按下刪除後必須刷新,看看是否刪除行或不刪除..在沒有頁面刷新的情況下刪除jquery中的行

這是我的查看代碼用戶名

  <td> <a href = "employesController/deleteEmploye/<?php echo $row->emp_id ?>" 
      class = "deletes" >Delete</td> 

我jQuery代碼

<script type='text/javascript'> 
    $(document).ready(function(){ 
$(".deletes").click(function(e){ 
    var r = confirm("are you sure you want to delete this"); 

    if (r==true){ 
    $this = $(this); 
    e.preventDefault(); 
    var url = $(this).attr("href"); 
    $.get(url, function(r){ 
     if(r.success){ 
      $this.closest("tr").remove(); 
     } 
    }) 
    }else { 
     return false; 
     } 

    }); 
    }); 

我控制器

function deleteEmploye($empid){ 

    $id = $empid; 
    $this->load->model('employesModel'); 
    $query = $this->employesModel->deleteEmploye($id); 
    return json_encode(array("success" => true)); 

模型

public function deleteEmploye($id){ 

     $this->db->where('emp_id', $id); 
     $query = $this->db->delete('employees'); 

     return $query->result(); 


} 
+0

你能表現出一點HTML –

+0

對沒錯.... ... – user1972143

+0

的''的$ .ajax' success'回調傳遞的數據從AJAX調用返回其第一參數。你確定你正在返回一個帶有'success'屬性的JSON對象,其值爲true嗎?此外,你泄露'$ this'作爲一個全球性的。添加'var'。 –

回答

0

你可以使用jQuery發送到另一個腳本中的PHP DELE TE該行並在confirn你可以刪除你的表

jQuery的

$.post("functions.php", { rowId: 1231 }, 
    function(data) { 
     if (data == "ok") 
      alert("the row has ben delete"); 
     else 
      alert("the row is not delete"); 

}); 

PHP

<?php 
    function delete_row(){ 
     //action to delete row in db 
     // when the row has bes delete you can print ok 
     echo 'ok'; 
     //if you have a error you can print a message that you can managed y jquery 
     echo "error 2121123" 
    } 
    if(isset($_post["rowId"])) 
     delete_row($_post["rowId"]);   
0

首先,你需要確認你的,如果條件塊調用的此行。如果它是正確調用然後試試這個:

$this.parent('td').parent('tr').remove(); 
1

使用Firebug(或者,如果你不使用火狐類似的工具)來調試Ajax調用。在「網絡」標籤中,你可以看到你的ajaxrequest是否真的被髮送,以及它返回的內容。

添加的console.log和console.dir語句代碼,找出發生了什麼:

$.get(url, function(r){ 
    console.log("returned form ajax"); 
    if(r.success){ 
     console.log("successful, $this is now "); 
     console.dir($this); 
     $this.closest("tr").remove(); 
    } 
} 

這樣你會發現,你的AJAX不會返回,但 不是你所期望的結果:它只返回數據, 所以查找r.success將永遠失敗。

您使用的方法只包含兩個參數: url和僅在成功時調用的函數! 所以你不需要if語句都:

$.get(url, function(r){ 
    $this.closest("tr").remove(); 
} 

對工作的完整示例見http://jsfiddle.net/bjelline/8RQQN/


另外:沒有沒有使用得到刪除的東西!改爲使用!你也應該考慮跨站請求僞造:在一個完全不同的網站有人可能添加一個鏈接

<a href="http://yoursite.com/employesController/deleteEmploye/4">free beer here</a> 

你欺騙用戶刪除員工 - 甚至沒有注意到!

https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29_Prevention_Cheat_Sheet

+0

..這是我得到的錯誤,在firbug響應..致命錯誤調用成員函數result()在非對象localhost/....在模態行38 – user1972143

+0

謝謝你的技巧,但我的事情我在模型中做錯了事情..我回來的結果..我做對了嗎? – user1972143

相關問題