2017-03-10 102 views
0

我有一個從數據表創建的表,如何在點擊複選框時更改狀態字段,默認狀態是'之前',然後當複選框點擊它時更新爲'之後'(在數據庫字段狀態),那麼該表重裝..當在PHP中點擊複選框時的更新狀態

這dataabs顯示錶

..... 
    foreach ($data as $key) { 
    // add new button, checkbox 
    $data[$i]['ceklolos'] = '<input type="checkbox"  id_data="'.$data[$i] ['status_lolos'].'" class="btn btn-primary btnedit" >'; 
     $i++; 
    ... 

如何的代碼的其餘部分,當複選框中的每個數據點擊「狀態之前,」從數據行的更新(默認數據庫)來待'狀態'後,表格重新加載..

謝謝,使用即時通訊的數據表和PHP

+0

你能否提供一個工作代碼 –

+0

PHP沒有複選框點擊。它是爲瀏覽器生成HTML的服務器端語言......一旦瀏覽器顯示頁面,就無法與PHP用於生成HTML的源代碼進行通信 - 它只是文本,畢竟沒有固有的功能。如果您想要將數據從瀏覽器發送到服務器,您需要調查諸如html表單或更高級的功能AJAX –

回答

1

首先,添加自定義數據屬性的複選框

<input type="checkbox" data-id="'.$data['id'].'" data-status="'.$data['status'].'" ../> 

在JavaScript,

// IIFE (Immediately Invoke Function Expressions) 
(function (myapp){ 
    myapp(window.jQuery, window, document); 
}(function myapp($, window, document){ 
    // $ is now locally scoped 
    $(function(){ 
     // dom is now ready 
     var dtTable = $("#sometable").DataTable(); 

     // dom events 
     $(document).on("change", '.btnedit', function(){ 
      var $this = $(this); 
      var id = $this.attr("data-id"); 
      var status = $this.attr("data-status"); 

      // send ajax request 
      $.ajax({ 
      url: 'path-to-your-php-file', 
      type: 'post', 
      dataType: 'json', 
      data: { 
       id: id, 
       status: status 
      }, 
      beforeSend: function(){ 
       // do something here before sending ajax 
      }, 
      success: function (data){ 
       // do something here 
       if(data.success){ 
        // update your table, or any html dom you want here 
        // if you want to add/remove rows from your dataTable, 
        // you can refer here 
        // https://datatables.net/reference/api/row.add() 
        // https://datatables.net/reference/api/row().remove() 
        // 
       } 
      }, 
      error: function (data){ 
       // do something here if error 
       // console.warn(data); 
      } 
      }); 
     }); 
    }); 
    // The rest of the code goes here 
})); 

在你的PHP文件,

<?php 

$id = $_POST['id']; 
$status = $_POST['status']; 
// do your update codes here 
// 
// after successful update return something so in your ajax you 
// will know what happened behind the scene 
$response = array('success' => false, 'msg' => 'Some error message'); 
if(some_update_function_success()){ 
    $response = array('success' => true, 'msg' => 'Some success message'); 
} 
echo json_encode($response); 
相關問題