选择行后如何删除行,然后按按钮

时间:2018-03-23 08:19:03

标签: javascript php jquery

截图

<?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&#34;值&#34;到php为了在数据库中做删除功能。我可以在这里使用$ .POST功能吗?或者在这里使用GET功能更好,但我认为它不安全。

2 个答案:

答案 0 :(得分:0)

如果没有得到参数,你可以这样做:

1 /你的FRONT:

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 /你的回复

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 /回到FRONT

你的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
},

这是你在找什么?

答案 1 :(得分:0)

这是一个简单的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