سؤال

I have a form that includes a delete button. I want the user to be able to select all the items they wish to delete at once. I managed to get the echo to display but the items still remain on the page. Can you please point out why that happens? Any help is appreciated!

Delete button:

  <input name="del_btn" type="submit" value="Delete">

The check:

   if (isset($_POST['del_btn']) && $_POST['del_btn']){
        for($i = 0; $i < count($_POST['checkbox']); $i++){
            if (isset($_POST['checkbox'])) {
                mysql_query("DELETE FROM `posts` WHERE `id` = '" . $_POST['checkbox'][$i] . "' LIMIT 1");   
                echo 'deleted';
            }
        }
 }

the items' checkbox:

<input name="checkbox[]" type="checkbox" id="<?php echo $id; ?>" />
هل كانت مفيدة؟

المحلول

This really depends on how variables are defined in your controller, and how they are displayed in your view file.

If you want to hide/clear the values in your forms you can:

  1. Reload the page: create a redirect at the end of your if (isset($_POST['del_btn']) && $_POST['del_btn']){ statement that sends the user back to the new page.
  2. Use jQuery to hide divs where the checkbox is checked.
  3. Redefine variables in your controller after the post to be NULL.
  4. Add a ternary operator to your values in the view so for example <input name="form_input" type="text" value=<?=isset($_POST['del_btn'] ? NULL : $form_input?>> which means that the form will display as NULL when the del_btn is submitted.

نصائح أخرى

<input name="checkbox[]" type="checkbox" id="<?php echo $id; ?>" />

I think you must put something like

<input name="checkbox[]" type="checkbox" value="<?php echo $id; ?>" />

Note the word "value" instead of "id". You are putting the ID in the id attribute. It is to assign a specific target if you want to take the value. (You can use Javascript or JQuery to do this).

In your case, use the tag "value" to ensure that you have correct ID value for your check box. Echo the query and not the word "deleted".

$queryDel =  "DELETE FROM posts WHERE id = " . $_POST['checkbox'][$i] . " LIMIT 1";
echo $queryDel;

Please beware of SQL Injection if you are using mysql_query(). Use mysqli instead. Use prepared statement to prevent it or at least provide some white list or validation.

Hope this helps. Thank you.

 <input name="checkbox[]" type="checkbox" value ="<?php echo $id; ?>" id="<?php echo $id; ?>" />

Try this it will work. and check the value print_r($_POST) you will able to see all the post value and able to get right value.

When you are using checkbox your element became an array than you can use multiple approaches:

Use ajax (example with jQuery) to avoid submit:

<script>
$(document).ready(function() {
   $('[name="del_btn"]').click(function() {
      $.post('/my/action.php', $('form').serialize(), function(r) {
         if (r.success) {
             alert('deleted....');
         } else {
             alert('error');
         }
      },'json');
      return false; // prevent submit
   });
});
</script>

Than you can simplify your code with this:

<?php
if (isset($_POST['checkbox']) && count($_POST['checkbox'])){
    $c = implode(',', array_map('intval', $_POST['checkbox'])); // sanitize data
    $r = mysql_query("DELETE FROM `posts` WHERE `id` IN(" . $c .")");
    echo json_encode(array('success' => $r));
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top