0

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>

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Form need to be multipart/form-data in this case.
Thanks for the correction. I didn't pay attention to this, because there is no form element in the code snippet.
Ok thanks gentlemen! So what would the code look like to call the variable and delete the selected items from the database?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.