2016-06-12 119 views
-1

我有幾行數據在運行時使用數據庫數據填充。每一行都有一個按鈕。點擊按鈕我想從數據庫中刪除該行。我在該按鈕上放了一個data-id。點擊按鈕發送一個ajax請求到php。但我在php中得到null id。以下代碼我使用。

HTMLcodeigniter input-> post()從jquery ajax發佈請求爲空

<span id="delete_btn" class="btn btn-default" data-id="<?php echo $id; ?>"> 
    <i class="glyph-icon icon-close"> Delete</i> 
</span> 

的JavaScript

$('#feedbackTable').on('click', '#delete_btn', function (event) { 

    var client_id = $(this).data('id'); 


    console.log(client_id); // I get the exact id 
    $.ajax({ 
     url: url + 'client/delete', 
     type: 'POST', 
     data: {'id' : client_id}, 
     dataType: 'json', 
     async: false, 
     cache: false, 
     contentType: false, 
     processData: false, 
     error: function (error_data) { 
      console.log(error_data); 
     }, 
     success: function (data) { 
      console.log(data); 
     } 
    }); 
    return false; 
}); 

Client.php

public function delete_client() { 
    if ($this->input->method(TRUE) == 'POST') { 
     $id = $this->input->post('id'); 

     if (is_numeric($id)) { 
      $id = intval($id); 
      echo json_encode(array(
       "is_error" => false, 
       "message" => $id 
      )); 
      return; 
     } else { 
      echo json_encode(array(
       "is_error" => true, 
       "message" => $id 
      )); 
      return; 
     } 
    } else { 
     echo json_encode(array(
      "is_error" => true, 
      "message" => "Invalid request" 
     )); 
     return; 
    } 
} 

JavaScript控制檯打印的用戶ID。 但在Ajax響應控制檯打印

Object {is_error: true, message: null} 

什麼,我做錯了什麼?對不起,我英文很差。 TIA

+0

你在AJAX使用'delete'方法,但你的方法名是'delete_client'。調整所有這些東西,然後檢查我的回答 –

+0

沒有具體原因,我從'route.php'的'delete'路由到'delete_client'。 – ARiF

回答

1

而不是使用is_numeric()您可以通過類型轉換轉換價值。嘗試以下

public function delete_client() { 
    if ($this->input->method(TRUE) == 'POST') { 
     $id = (int) $this->input->post('id'); 

     if (!empty($id)) { 
      echo json_encode(array(
       "is_error" => false, 
       "message" => $id 
      )); 
      return; 
     } else { 
      echo json_encode(array(
       "is_error" => true, 
       "message" => $id 
      )); 
      return; 
     } 
    } else { 
     echo json_encode(array(
      "is_error" => true, 
      "message" => "Invalid request" 
     )); 
     return; 
    } 
} 

從阿賈克斯

$('#feedbackTable').on('click', '#delete_btn', function (event) { 

    var client_id = $(this).data('id'); 


    console.log(client_id); // I get the exact id 
    $.ajax({ 
     url: url + 'client/delete', 
     type: 'POST', 
     data: {'id' : client_id}, 
     dataType: 'json', 
     error: function (error_data) { 
      console.log(error_data); 
     }, 
     success: function (data) { 
      console.log(data); 
     } 
    }); 
    return false; 
}); 

下列卸下其次刪除不必要的PARAMS。

 async: false, 
     cache: false, 
     contentType: false, 
     processData: false, 

contentTypeprocessData等使用時,您的形式包含圖像等等等等,這裏不要求

+1

我不認爲'is_numeric()'有問題,但是'json'和'post'請求有問題,因爲在php'post'方法中'id'是'null'。從ajax中刪除這些參數。非常感謝 ! – ARiF

0

嘗試

var client_id = $(this).attr('data-id'); 

,而不是

var client_id = $(this).data('id'); 
+0

謝謝,我試過兩種方法。仍是同樣的問題。我在'console.log(client_id)'''行中正確獲取了jquery中的'client_id'。但在PHP'ID'變爲空。 – ARiF

0

首先嚐試發送Ajax請求不附加與沿單擊事件{過於:「遠」}然後檢查數據爲服務器端的酒吧和回覆我。 想知道爲什麼多個元素具有相同的屬性。

<span id="delete_btn" class="btn btn-default".. 

而不是 類= 「delete_btn」

+0

我的錯誤。儘管多個'id'具有相同的名稱。我會改變這一點。謝謝 ! 而這種改變並沒有解決問題。:( – ARiF

+0

我告訴你發送一個虛擬數據到該網址並檢查響應,而不是點擊事件 – Wakeel

+0

哦,對不起!檢查它。 – ARiF