【问题标题】:Delete button for each row in a table PHP表格PHP中每一行的删除按钮
【发布时间】:2016-02-26 21:24:37
【问题描述】:
<td style="text-align: left;">
    <?php
          echo '<button type="button" class="btn btn-danger delete delete-account"><i class="fa fa-trash-o fa-lg"></i> Delete</a></button> 
                <input type="hidden" value="'. $r['user_id'] .'" name="delete[]">';
    ?>
</td>

我有这段 javascript 代码:

$('button.delete-account').click(function(e) {
    swal({
        title: "Are you sure?",
        text: " <?php echo $r['user_id'] ?> You will not be able to undo this acction !",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, delete it!",
        closeOnConfirm: false,
        html: false
    }, function() {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
    });
});

$r['user_id']---> 这基本上是一个帮助我从数据库中提取所有用户的函数,对于这个示例,它将提取所有 user_id。

问题是我在每一行都有不同的用户,当我点击删除按钮时,我得到的是相同的 user_id 而不是个人 id。

我怎样才能单独呼叫每个用户???

谢谢大家:)

【问题讨论】:

  • 为什么每一行都有多个用户?
  • 哦,对不起,我不是说每行一个用户,而是每个用户在不同的行
  • 我已经更新了我的问题,抱歉
  • 你能给我们看看你的桌子吗?截图或html
  • 为什么不在删除按钮上设置值.. 这样你就可以通过在 jquery 中获取按钮的值来知道要删除哪个用户

标签: javascript php jquery html-table


【解决方案1】:

更新:无法完全理解您的代码,但您可以通过查看这个小提琴了解我的想法:Example

使用创建的按钮的值设置用户的 id 是一种方法。这样,您将为每个创建的按钮分配一个值,该值代表用户的 id ..

<td style="text-align: left;">
    <?php
          echo '<button type="button" value="'. $r['user_id'] .'" class="btn btn-danger delete delete-account"><i class="fa fa-trash-o fa-lg"></i> Delete</a></button>';
    ?>
</td>

然后在你的jquery中通过使用jquery的$(this)获取被点击的按钮的值来获取被点击的按钮并通过调用.val()获取值强>。这样,您将只有一个处理程序来处理所有创建的按钮。 :

$('button.delete-account').click(function(e) {
swal({

title: "Are you sure?",
text: $(this).val()+" You will not be able to undo this acction !",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false,
html: false
}, function(){
swal("Deleted!",
"Your imaginary file has been deleted.",
"success");
});
});

代码未测试 ...我不太能够验证您的代码是如何工作的,所以我只是复制了您的代码并用按钮的值进行了微小的更改..但大多数重要的是让您了解在每个按钮上存储用户的值(id),并在您的处理中使用 $(this) 检索它..

【讨论】:

    【解决方案2】:

    您也可以从输入中获取 id(我假设您使用的是 jquery)

    var id = $(e).parent().find('input[type="hidden"]').val();
    

    未测试

    【讨论】:

      【解决方案3】:

      这两个脚本就可以了。

      显示.php

      <?php
      /* 
      
      Deletes a specific entry from the  table
      */
      
      // connect to the database
      include('connect-db.php');
      
      // check if the 'id' variable is set in URL, and check that it is valid
      if (isset($_GET['id']) && is_numeric($_GET['id']))
      {
      // get id value
      $id = $_GET['id'];
      
      // delete the entry
      $result = mysql_query("DELETE FROM *fillin* WHERE id=$id")
      or die(mysql_error()); 
      
      // redirect back to the view page
       header("Location: urlocation.php");
       }
      else
       // if id isn't set, or isn't valid, redirect back to view page
       {
      header("Location: urlocation.php");
      }
      
      ?>
      

      删除.php

          <?php
          */  Displays all data from ' table
      
      
          // connect to the database
          include('dbconnectfile.php');
      
          // get results from database
          $result = mysql_query("SELECT * FROM dbase") 
                  or die(mysql_error());  
      
            // display data in table
        echo "<p><b>info</b></p>";
      
          echo "<table border='2' border-color='#800000' cellpadding='10'>";
          echo "<tr> <th>fill-in</th> <th>fill-in</th><th>fill-in</th><th>Delete</th></tr>";
      
          // loop through results of database query, displaying them in the table
          while($row = mysql_fetch_array( $result )) {
      
                  // echo out the contents of each row into a table
                  echo "<tr>";
                  echo '<td>' . $row['rowname'] . '</td>';
                  echo '<td>' . $row['rowname'] . '</td>';
                  echo '<td>' . $row['rowname'] . '</td>';
      
                  echo '<td><a HREF="delete.php?id=' . $row['id'] . '"><img style="width: 25px; height: 25px; " alt="" src="">Delete</a></td>';
                  echo "</tr>"; 
          } 
      
          // close table>
          ?>
      

      【讨论】:

        【解决方案4】:

        在 html 上:

        <td style="text-align: left;">
                <?php
                      echo '<button type="button" class="btn btn-danger delete delete-account"><i class="fa fa-trash-o fa-lg"></i> Delete</a></button> 
                            <input type="hidden" class="userId" value="'. $r['user_id'] .'" name="delete[]">';
                ?>
            </td>
        

        关于js:

        $('button.delete-account').click(function(e) {
           var id = $(this).siblings('input.userId').val();
          swal({
           title: "Are you sure?",
           text: id + " You will not be able to undo this acction !",
           type: "warning",
           showCancelButton: true,
           confirmButtonColor: "#DD6B55",
           confirmButtonText: "Yes, delete it!",
           closeOnConfirm: false,
           html: false
          }, function(){ 
           $.post( "delete.php", { userID: id } );
           swal("Deleted!",
             "Your imaginary file has been deleted.",
             "success");  
          });
        });
        

        关于删除.php:

        <?php
        if(isset($_POST['userID'])){
          $id = $_POST['userID'];
          include('connect-db.php');
          mysql_query("DELETE FROM table_name WHERE userID = " . $id);
        }
        ?>
        

        【讨论】:

          猜你喜欢
          • 2013-04-17
          • 1970-01-01
          • 1970-01-01
          • 2017-09-03
          • 2021-09-09
          • 1970-01-01
          • 2020-06-22
          • 2012-07-18
          相关资源
          最近更新 更多