首页 > 解决方案 > 使用 PHP POP 删除模式删除操作不起作用?

问题描述

我正在使用 PHP 模式来确认从表中删除记录,但它不起作用。弹出模式即使变成提交也会出现,但不幸的是记录没有被删除。知道为什么无法完成此操作。请为我提供确切的解决方案,我能做些什么来解决问题。

<?php
  if(isset($_POST['deletedata']))
  {  
    $id = $_POST['delete_id'];
    mysqli_query($db, "DELETE FROM gallery WHERE id=$id");
    header("Location:gallery.php"); 
  }
?>
<script>
  $(document).ready(function() {
    $(".deletebtn").on('click', function() {
      $(".deletemodal").modal('show');
      var deletedId = $(this).closest("tr").find(':first-child').text();
      $("#delete_id").val(deletedId);
    });
  });
</script>
 <!---POP Delete Modal --->
 <div class="modal fade" id="deletemodal" class="deletemodal"role="dialog" 
 aria-hidden="true" ><div class="modal-dialog" >
 <div class="modal-content"><div class="modal-header">
            <button type="button" class="close" 
            data-dismiss="modal" aria-hidden="true">×</button>
            <h2 class="modal-title">Delete Image</h2>
        </div>
        <form action="gallery.php" method="post" enctype="multipart/form- 
        data">
            <div class="modal-body">
                <input type="hidden" name="delete_id"  id="delete_id">
                <p>Are you sure you want to delete this image?</p>
            </div>
            <div class="modal-footer">
                <button type="submit" style="margin-right:-5px;" 
              name="deletedata" class="btn btn-primary" >Yes</button>
                <button type="button"  style="margin-top:-2px;" class="btn 
         btn-default " data-dismiss="modal">No</button>
            </div>
        </form></div></div> </div>
<!---Gallery Table Start --->
<div class="candile">
  <div class="candile-inner" style="margin-top:-40px;">
    <h3 style="color:#002561">Gallery</h3>
    <button type="button" data-toggle="modal" data-target="#gmodal" 
    class="btn btn-lg btn-default">ADD</button>
    <div class="graph">
      <div class="tables">
        <table class="table table-bordered">
          <thead>
            <tr>
              <th class="th-sm">ID</th>
              <th class="th-sm">Title</th>
              <th class="th-sm">Content</th>
              <th class="th-sm">Image</th>
              <th class="th-sm">Edit</th>
              <th>Delete</th>
            </tr></thead><tbody>
            <?php
while($datarecord=mysqli_fetch_assoc($resultrecord))?>
              <tr>
                <td style="text-align:center;">
                  <?php echo $datarecord["id"]; ?>
                </td>
                <td style="text-align:center;">
                  <?php echo $datarecord["title"]; ?>
                </td>
                <td>
                  <?php echo $datarecord["content"]; ?>
                </td>
                <td style="text-align:center;">
                  <?php echo '<img src="images/'.$datarecord['gimage'].'">';?></td>
                <td style="text-align:center;"><input type="button" data-toggle="modal" data-target="#editegmodal" class="btn btn-lg btn-warning warning_11 
  editebtn" id="editegbtn" value="Edit"></td>
                <td style="text-align:center;"><input type="button" value="Delete" data-toggle="modal" data-target="#deletemodal" class="btn btn-lg btn- 
  danger deletebtn" id="deletebtn"></td>
              </tr>
              <?php}?></tbody></table></div></div> </div></div>

标签: javascriptphpjquerymysql

解决方案


change edit button with attr id
<input type="button" value="Delete" data-toggle="modal" data- 
target="#deletemodal" class="btn btn-lg btn- 
danger deletebtn" id="deletebtn" id="<?php echo $datarecord["id"]; ?>">

change button event
var deletedId = $(this).attr("id");

推荐阅读