簡體   English   中英

選擇行並按下按鈕后如何刪除行

[英]How to delete row after i select the row and press the button

屏幕截圖

<?php 
if(isset($_GET['View']) && $_GET['View']=="HistoryEntry"){    
    echo '    
              <h2>History Of Entries</h2>               
                <table id="table" class="table table-hover">
                    <thead>
                      <tr>
                        <th scope="col">#</th>
                        <th scope="col">Date In</th>
                        <th scope="col">Date Out</th>
                        <th scope="col">Rfid</th>
                        <th scope="col">Plate #</th>  
                     </tr>
                    </thead>
                    <tbody>';

 global $connection;  
$query = "SELECT * FROM history_entries";

$result = mysqli_query($connection, $query); 
while($row = mysqli_fetch_assoc($result)){

    echo '<tr>
                        <th scope="row">'.$row['Entry_ID'].'</th>
                        <td>'.$row['Date_in'].'</td>
                        <td>'.$row['Date_out'].'</td>
                        <td>'.$row['Acc_rfid'].'</td>
                        <td>'.$row['Plate_num'].'</td>


          </tr>';

}

         echo '          </tbody>
                  </table>
                  <center>
                  <button>Delete</button>
                      </center>
                <div class="line"></div>';
} 

?>

?>
$("#table tr").click(function() {
  $('.selected').removeClass('selected');
  $(this).addClass("selected");
});

$("#Sample").click(function() {
  var value = $(".selected th:first").html();
  value = value || "No row Selected";
});

如您所見,這是我的代碼,我已經知道如何選擇行並獲取ID,但不能將ID“值”傳遞給php,以便在數據庫中執行刪除功能。 我可以在這里使用$ .POST函數嗎? 還是在這里使用GET函數更好,但我認為這並不安全。

這是您可以在不獲取參數的情況下執行的操作:

1 /您的前臉:

HTML(僅作為示例)

<table>
    <tr id="row_id">
        <td>Data 1</td>
        <td>Data 2</td>
        ...
    </tr>
    ...
</table>

<button id="delete_row" type="button">Delete</button>

JS / jQuery

var current_row_id = "";

// You select the row
$("#table tr").click(function() {
    $('.selected').removeClass('selected');
    $(this).addClass("selected");

    current_row_id = $(this).attr("id"); // Here you get the current row_id
});

// You delete the row
$("#delete_row").on("click", function() {
     $.ajax({
         type: "POST",
         url: "delete.php",
         data: {"id" : current_row_id }, // You send the current_row_id to your php file "delete.php" in post method
         dataType: 'json', // you will get a JSON format as response
         success: function(response){
             // you do something if it works       
         },
         error: function(x,e,t){
             // if it doesn't works, check the error you get
             console.log(x.responseText);
             console.log(e);
             console.log(t);
         }
    });
});

2 /您的BACK

PHP“ delete.php”文件

<?php

$id = $_POST['id']; // You get the 'current_row_id' value

// Now you do your DELETE request with this id :
$sql = "DELETE ... WHERE id = :id"; 

etc...

$result = array(); // You can prepare your response and send information about what you did

$result['row_deleted'] = $id;
$result['message'] = "The row with id = " . $id . " was deleted with success";
$result['type'] = "success";
//etc....

echo json_encode($result); // You send back a response in JSON format

3 /返回正面

您的ajax電話,成功部分:

success: function(response){
    // You can display a success message for example using your response :

    alert(response.message); // You will get 'The row with id = {the row id} was deleted with success' here for example
},

是您要找的東西嗎?

這是一個簡單的Ajax請求

var data = {
   rowId: 1
};

$.ajax({
  type: "POST",// GET|POST
  url: 'delete.php', // Where you want to send data like url or file
  data: data, // this is what you want to send to server, in this case i send data with id = 1 to server
  dataType: 'json' // here we say what data type we want "json"
  success: function(response) {
      alert(response);
  }, // this is the callback were u will get response from your server
});

delete.php這是您如何處理此ajax

$rowId = htmlspecialchars($_POST['rowId']);
if ($rowId) {
    global $connection;
    $query = "DELETE FROM history_entries WHERE Entry_ID = " . $rowId;
    $result = mysqli_query($connection, $query); 

    $response = array(
        'success' => true 
    );
    echo json_encode($response);
    exit;

} else {
  echo json_encode(array('success' => false));
  exit;
}

希望這會幫助您了解如何使用Ajax

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM