dongyu1614 2009-12-18 12:36
浏览 76

如果选中复选框,则删除多行

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>
";
      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>
  • 写回答

1条回答 默认 最新

  • duanhanglekr37902 2009-12-18 13:11
    关注

    You need to modify 'checkbox' element in order to make an array of values. Code:

    echo "<input name='checkbox[]' type='checkbox' id='checkbox' value='$question_id'>";
    

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器