2014-08-31 32 views
0

我想刪除我的html表中的字段。當我點擊刪除按鈕時,我將用戶重定向到一個彈出窗口,詢問他們是否確定是否要刪除表格中的那一行。一旦他們點擊yes,它會將它們帶到一個delete.php文件中,該文件將它從我們的數據庫中刪除。我不知道如何將這個值發送到我們的delete.php文件。發送表數據彈出刪除

<!-- /.row --> 
    <div class="row"> 
    <div class="col-lg-12"> 
    <div class="panel panel-default"> 
     <div class="panel-body"> 
    <div class="table-responsive"> 
    <table class="table table-striped table-bordered table-hover" id="dataTables-example"> 
    <thead> 
    <tr> 
    <th>User Name</th> 
    <th>Distinct Items</th> 
    <th>Total Occurrences</th> 
    <th>Last Occurrence</th> 
    <th>Environment</th> 
    <th>Control</th> 
    </tr> 
     </thead> 
     <tbody> 


     <?php 

     include ("config.php"); 

     $sql = "SELECT * FROM newTable"; 
     $result = mysql_query($sql); 
     while($row = mysql_fetch_array($result)) { 
     echo '<tr>'; 
     echo '<td>'. $row['account_id'] . '</td>'; 
     echo '<td>'. $row['user'] . '</td>'; 
     echo '<td>'. $row['pass'] . '</td>'; 
     echo '<td>'. $row['privs'] . '</td>'; 
     echo '<td>'. $row['privValue'] . '</td>'; 
     echo '<td>'; 
     echo '<a href="#Modal_modify" class="btn btn-primary btn-xs" data-toggle="modal"><i class="fa fa-pencil"></i></a>'; 
     echo '<a href="#Modal_delete" class="btn btn-danger btn-xs" data-toggle="modal"><i class="fa fa-trash-o "></i></a>'; 
     echo '</td>'; 
     echo '</tr>'; 
    } 
?> 





<!-- Modal_delete HTML --> 
     <div id="Modal_delete" class="modal fade"> 
      <div class="modal-dialog"> 
       <div class="modal-content"> 
        <div class="modal-header"> 
         <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
         <h4 class="modal-title">Delete User</h4> 
        </div> 
        <div class="modal-body"> 
         <p>Are you sure you want to delete this user?</p> 
        </div> 

        <form action="delete_user.php" method="post"> 
         <input type="hidden" name="id" value="<?php echo $id;?>"/> 
         <div class="modal-footer"> 
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
          <button type="submit" class="btn btn-danger">Delete</button> 
         </div> 
        </form> 

       </div> 
      </div> 
     </div> 

問題是創建刪除按鈕我引用一個html子例程來創建一個被點擊的彈出窗口。我如何將表格數據發送到我的delete.php文件?

<?php 
include ("test.php"); 

$tbl_name="newTable"; // Table name 

$id = $_POST['id']; 

echo $_POST['id']; 

echo "$id"; 
// To protect MySQL injection (more detail about MySQL injection) 

$sql="DELETE FROM tbl_name WHERE account_id='$id';"; 
mysql_query(sql); 

header("Location: adminUser.php"); 




?> 

回答

0

你是否反對使用jQuery? (很遺憾,我會問,在評論事前,但是,嘖,沒有足夠的聲譽,所以這裏是我的全部答案:)下面是非常簡單的:

$('.btn-danger').click(function(event) { 
    $('#Modal_delete').css('display', 'initial'); // swap this for however you're showing your delete confirmation popup. I just had it for testing. 
    var rowToDeleteID = $(event.currentTarget).parents('tr').attr('id'); // you'd need to add a unique id to each table row for this to work. 
    $('input[type="hidden"]').val(rowToDeleteID); 
}); 

當然,這將是更強大的(實際上,取決於你的其他代碼,它可能只有工作[更不用說它只是一個好主意]),如果你添加唯一的標識符到刪除按鈕(a標籤與fa-trash-o圖標)和您的隱藏輸入字段。

0

可以使用$row['account_id']作爲鏈接#Modal_delete的屬性和使用jQuery得到它..

<a href="#Modal_delete" class="btn btn-danger btn-xs" data-toggle="modal" data-account-id="<?= $row['account_id'] ?>"><i class="fa fa-trash-o "></i></a> 

$("a[href='#Modal_delete']").on("click", function(e) { 
    $("input[name='id']").val($(this).data("account-id")); 
}); 
+0

好感謝輸入所以我應該使用jQuery來獲取價值。我怎麼會真的把它發送到我的PHP文件? – user3404290 2014-08-31 05:23:49

+0

您只需提交表格,就像您今天所做的那樣。 '$ _POST [「id」]'將從隱藏的輸入'name ='id''中獲得新的值。你可以從id輸入中刪除'value =「<?php echo $ id;?>」'。 – 2014-08-31 23:40:58

+0

嗯我加了你說的,價值還沒有通過 – user3404290 2014-09-01 00:15:28