首页 > 解决方案 > 如何从复选框中获取选定的 ID 到选择选项中?

问题描述

我有一个显示用户的表格,我在其中添加了选择所有复选框以删除选定的用户或发送电子邮件。

这就是我收集 id 的方式:<input type="checkbox" class="check" name="del[]" value="<?php echo $author['user_id']; ?>">

我在表格下方添加了一个选择选项,其中包含两个选项,删除或发送电子邮件。

当我选择删除时,它应该删除选定的 ID,如果我选择发送电子邮件,那么它应该返回电子邮件功能。

问题是我无法将选定的 ID 放入选择选项中。

注意:php 和 html 代码在同一个页面

问题:如何从复选框中获取选定的 id 到选择选项中?

这是我的表格代码:

<form action="" method="post">
   <thead>
       <tr class="table-success">
       <th scope="col">
           <div class="checkbox">
             <label>
               <input type="checkbox" class="check" id="checkAll">
             </label>
           </div>
        </th>
           <th scope="col">Username</th>
           <th scope="col">Name</th>
           <th scope="col">Email</th>
           <th scope="col">Role</th>
           <th scope="col">status</th>
       </tr>
   </thead>
   <tbody>
       <?php foreach($authors as $author): ?>
       <tr>
          <th scope="row">
              <div class="checkbox">
                <label>
                  <input type="checkbox" class="check" name="del[]" value="<?php echo $author['user_id']; ?>">
                </label>
              </div>
           </th>
           <td><?php echo $author['username']; ?></td>
           <td><?php echo $author['user_fname']; ?></td>
           <td><?php echo $author['user_email']; ?></td>
           <td><?php echo $author['user_role']; ?></td>
           <td><?php echo $status; ?></td>
       </tr>
       <?php endforeach; ?>
   </tbody>
   <tfoot>
       <tr class="table-success">
          <th scope="col">
              <div class="checkbox">
                <label>
                  <input type="checkbox" class="check" id="checkAll">
                </label>
              </div>
           </th>
           <th scope="col">Username</th>
           <th scope="col">Name</th>
           <th scope="col">Email</th>
           <th scope="col">Role</th>
           <th scope="col">status</th>
       </tr>
       <tr>
       <th colspan="6">
        <select name="users_operation">
           <option value="" disabled selected>Choose option</option>
           <option value="delete" id="delete">username</option>
           <option value="sendMail" id="sendMail">Send Email</option>
         </select>

          <input type="submit" name="submit" value="Send">
         </th>
       </tr>
   </tfoot>
</form>

PHP代码:

if(isset($_POST['submit'])){
 
 if(!empty($_POST['del'])) {
        $selectedIds = $_POST['del'];
 }
 if(!empty($_POST['users_operation'])) {
    if(!empty($_POST['delete'])) {
       //Do query
        print_r($_POST['del']);
        
    } elseif(!empty($_POST['sendMail'])) {
       // Send email
        print_r($_POST['del']);
        
    } else {
        echo 'Please select the value.';
    }
 }
}

谢谢你的帮助

标签: php

解决方案


推荐阅读