2014-02-10 49 views
0

希望你今天做得很好。 ;)PHP - 對動態創建的「刪除按鈕」進行反應

首先,這裏是我的問題:從我的數據庫表中一個HTML表動態

林加載數據。到現在爲止還挺好。 我的HTML表格中的數據的動態加載工作得很好...

現在,我想檢查是否按下刪除按鈕,並且還希望按下刪除按鈕的記錄中的ID。

HTML表具有以下值: ID,名字,姓氏,單選按鈕(要選擇應稍後刪除的記錄),刪除 - 按鈕(刪除所選的記錄)

這裏是我的源代碼:

<?php 

$query = $con->prepare("SELECT id, vorname, nachname, herkunftsland FROM schauspieler;"); 
$query->bind_result($id, $firstname, $lastname, $city); 
$query->execute(); 

while($query->fetch()) { 

    $actor_information[$id]['id'] = $id; 
    $actor_information[$id]['firstname'] = $firstname; 
    $actor_information[$id]['lastname'] = $lastname; 
    $actor_information[$id]['city'] = $city; 
} 

?> 

<html> 
<head> 
    <title></title> 
</head> 
<body> 

<p></p> 
<h4>Actors:</h4> 
<hr /><br /> 

<!-- 
<table border="3"> 
<tr> 
    <td><input type="radio" name="1"></input></td> 
    <td><input type="radio" name="1"></input></td> 
    <td><input type="radio" name="1"></input></td> 
    <td><input type="radio" name="1"></input></td> 
</tr> 
</table> 
--> 

<form action="" method="post"> 
<table border="3"> 
    <th>id</th> 
    <th>FirstName</th> 
    <th>LastName</th> 
    <th>City</th> 
    <?php 

    foreach($actor_information as $key => $value) { 

     echo "<tr>"; 
     printf("<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><input type='radio' name='dvd_id' value='%s'></input></td><td><input type='button' name='delete_actor' value='delete'></input></td>", $value['id'], $value['firstname'], $value['lastname'], $value['city'], $value['id']); 
     echo "<tr>"; 
    } 

    ?> 
</table> 
</form> 

任何人都可以幫我從選定的記錄中獲得ID ...?我只需要它刪除單選按鈕設置的記錄。

希望我能得到你們一些幫助......

問候&有一個愉快的一天, 彼得

+0

你必須使用ajax和jQuery爲此目的 – krishna

回答

0

什麼是第一行有這4個單選按鈕的想法?

我在應用程序中實現這一點的方式是在每行的第一列中添加一個X圖像,以打開一個新的彈出窗口(可能在確認之後並且使用離屏座標,因此用戶看不到它)其中調用delete_record.php?id=$id -URL。

+0

刪除數據時使用URL參數不是一個好主意,Google會抓取油墨並給你無盡的頭痛...... –

+0

謝謝 - 你是對的。我一直在說內聯網,並希望爬蟲不會跟隨JS中嵌入的鏈接。 – MBaas

0

您可以使用刪除鏈接,並將其樣式設置爲按鈕(see)。鏈接位置是當前網址+參數'delete_resource',as值爲行ID。

在頁面頂部,檢查參數'delete_resource'是否已設置,並根據該ID刪除行。

0

如果您給刪除按鈕一個「名稱」屬性,它將包含在發佈數據中。因此,你可以檢查它的存在並採取行動。

下面的PHP提供了一個文本輸入和兩個不同的提交按鈕,每個按鈕都有不同的名稱。它還打印$_POST的內容,因此您可以根據所按的按鈕查看它的更改方式。

<!DOCTYPE html> 
<html> 
<head><title>Test post!</title></head> 
<body> 

<?php var_dump($_POST); ?> 

<form method="post"> 
    <input name="test" type="text" value="Test" /> 
    <input type="submit" name="delete" value="Delete!" /> 
    <input type="submit" name="submit" value="Submit!" /> 
</form> 
</body> 
</html> 

在特定情況下,應該足夠使用不同的名稱屬性爲每個演員,例如使用 name='delete_".$value["id"]."'"或相似。

我似乎誤解了這個問題。如果您只使用一個刪除按鈕,爲每條記錄添加一個項目符號,並且項目符號的值是要刪除的記錄的編號,則只需檢查$_POST中的刪除按鈕的名稱,然後使用任何數據庫代碼您正在使用刪除與$_POST["dvd_id"]匹配的記錄。

0

我會用jquery ajax來做這件事,一個收音機只打算有一個可選擇的選項,一個複選框就是你需要按照你打算的方式來做這件事。然而,如果你認爲你將不得不點擊每一行,那麼取消表單並在每行上添加一個帶有data-id ='%s'的按鈕以攜帶id並通過jquery進行觸發將會更簡單。

<?php 
$query = $con->prepare("SELECT id, vorname, nachname, herkunftsland FROM schauspieler;"); 
$query->bind_result($id, $firstname, $lastname, $city); 
$query->execute(); 

while($query->fetch()) { 
    $actor_information[$id]['id'] = $id; 
    $actor_information[$id]['firstname'] = $firstname; 
    $actor_information[$id]['lastname'] = $lastname; 
    $actor_information[$id]['city'] = $city; 
} 

?> 


<!DOCTYPE html> 
<html> 
<head> 
    <title>Some Page Title</title> 
    <script src="//code.jquery.com/jquery-1.10.2.js"></script> 
</head> 
<body> 

<table border="3"> 
    <th>id</th><th>FirstName</th><th>LastName</th><th>City</th><th>Option</th> 
<?php 
foreach($actor_information as $key => $value) { 
    echo '<tr class="user-row">'; 
    printf('<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><button class="delete-user" data-id="%s">Delete </button></td>', $value['id'], $value['firstname'], $value['lastname'], $value['city'], $value['id']); 
    echo '<tr>'; 
} 

?> 
</table> 

<script type="text/javascript"> 
    jQuery(document).ready(function($){ 
     $('.user-row').each(function() { 
      $(this).find('.delete-user').click(function(e){ 
       var user_id = $(this).data('id'); 
       var callback = function (response) { 
         alert(response); 
         $(this).parent('.user-row').remove(); 
       } 
       delete_user(user_id,callback); 
      }); 
     }); 
    }); 

function delete_user(user_id,callback){ 
var ajax_url = "/path/to.php"; 
    $.ajax({ 
    type: "POST", 
    url: ajax_url, 
    data: { user_id:user_id }, 
    context:this, 
    success: function(response) { 
       callback(response); 
      } 
    });    
}; 
</script> 

</body> 
</html> 

該值可以通過$ _POST ['user_id'];並傳遞到MySQL刪除然而,要注意,作爲一種這樣的方法沒有一些衛生設備的MySQL注入可以打開你的數據庫到黑客