如果选中复选框,则删除多行
我想构建一个功能,使用户能够审核问题,类似于 WordPress 评论或典型的电子邮件审核,使用复选框一次批准/删除许多问题。
我如何修改以下代码来检查哪些问题被选中并对按下删除按钮做出反应?您可能会从下面的代码中注意到,我目前可以针对个别问题执行此操作,但我想赋予用户更多权力。
<h2><a href="#">Moderate Questions</a></h2>
<div>
<button type="button" class="btn" name="delete" id="delete">Delete</button>
<button type="button" class="btn" name="approve" id="approve">Approve</button>
<?php
// Select all unapproved questions in db
$sql = "SELECT question_id, question, DATE_FORMAT(question_date, '%e %b %Y at %H:%i') AS dateattime FROM questions WHERE question_approved='0' ORDER BY question_date DESC";
$result = mysql_query($sql);
$myquestions = mysql_fetch_array($result);
if($myquestions) {
do {
$question_id = $myquestions["question_id"];
$question = $myquestions["question"];
$dateattime = $myquestions["dateattime"];
echo "<div class='question'>";
echo "<span value='$question_id'>";
echo "<input name='checkbox' type='checkbox' id='checkbox' value='$question_id'>";
echo "$question - <span class='date'>Asked $dateattime </span>";
echo "</span>\n";
echo "<div class='question-controls'><a href='".$_SERVER["PHP_SELF"]."?delete=$question_id' title='Delete this question' class='delete' onclick='return confirm(\"Are you sure you want to delete this question?\")'>Delete</a> | <a href='#'>Edit</a> |";
echo " <a href='".$_SERVER["PHP_SELF"]."?approve=$question_id' title='Publish this question'>Approve</a></div>";
echo "</div>";
} while ($myquestions = mysql_fetch_array($result));
} else {
echo "<p>No one has asked a question recently.</p>";
}
?>
</div>
I want to build a feature that enables the user to moderate questions and is similar to wordpress comment or typical email moderation that uses a checkbox to approve/delete many questions at once.
How would I modify the following code to check which the questions are checked and react to the delete buttons when pressed? You may notice from the below code that I can currently do this with individual questions but I want to give the user more power.
<h2><a href="#">Moderate Questions</a></h2>
<div>
<button type="button" class="btn" name="delete" id="delete">Delete</button>
<button type="button" class="btn" name="approve" id="approve">Approve</button>
<?php
// Select all unapproved questions in db
$sql = "SELECT question_id, question, DATE_FORMAT(question_date, '%e %b %Y at %H:%i') AS dateattime FROM questions WHERE question_approved='0' ORDER BY question_date DESC";
$result = mysql_query($sql);
$myquestions = mysql_fetch_array($result);
if($myquestions) {
do {
$question_id = $myquestions["question_id"];
$question = $myquestions["question"];
$dateattime = $myquestions["dateattime"];
echo "<div class='question'>";
echo "<span value='$question_id'>";
echo "<input name='checkbox' type='checkbox' id='checkbox' value='$question_id'>";
echo "$question - <span class='date'>Asked $dateattime </span>";
echo "</span>\n";
echo "<div class='question-controls'><a href='".$_SERVER["PHP_SELF"]."?delete=$question_id' title='Delete this question' class='delete' onclick='return confirm(\"Are you sure you want to delete this question?\")'>Delete</a> | <a href='#'>Edit</a> |";
echo " <a href='".$_SERVER["PHP_SELF"]."?approve=$question_id' title='Publish this question'>Approve</a></div>";
echo "</div>";
} while ($myquestions = mysql_fetch_array($result));
} else {
echo "<p>No one has asked a question recently.</p>";
}
?>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要修改“checkbox”元素才能创建值数组。代码:
之后你可以从 php 代码调用这个变量(例如使用 $_REQUEST['checkbox']),它将是数组。因此您可以迭代它并从数据库中删除这些行。
You need to modify 'checkbox' element in order to make an array of values. Code:
After that your can call this variable from php code(using $_REQUEST['checkbox'] for example) and it will be array. So your can iterate through it and delete these rows from database.