php - 如何从复选框中获取选定的 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.';
}
}
}
谢谢你的帮助
解决方案
推荐阅读
- sql - 如何比较两列之间的字符串的一部分
- git - 为什么 git rev-parse --show-toplevel 返回与实际路径不同的大小写
- vue.js - 用于 SketchUp 的 Vue.js 界面
- c# - 从数据库中检索数据读取器时的免费 UI 并指示数据检索是否完成 C#
- android - 片段中的 CameraView.setLifecycleOwner(this) 回调
- http - 使用 PySpark 并行和异步运行 HTTP 请求
- c++ - 对于 wcin 中包含的相同数量的无效字符,Failcheck 消息会重复
- laravel - Laravel - 使用 Google 2FA 保护所有视图
- python - 谷歌分析、服务器端跟踪和机器人过滤器
- c++ - 有带默认参数的构造函数时是否有 2 次初始化