1

I am trying to create a function where users can check a message to delete. Obviously if more than one message is checked, they will all be deleted.

I have the following code

<form action="process.php" method="post">
    <input type="hidden" name="deleteMessages" />
    <?php
    while($row=mysql_fetch_assoc($q))
    {
        if($row['to_viewed'] == 0)
        {
        ?>
        <tr>
            <td><input type="hidden" name="check_box_1" value="0" />
                <input type="checkbox" name="check_box_1" value="1" />
            </td>
            <td><b><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['title'] ?></a></b></td>
            <td><b><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['from']; ?></a></b></td>                
            <td><b><?php echo $row['created']; ?></b></td>
        </tr>
        <?php
        }
        else if($row['to_viewed'] == 1)
        {
        ?>
        <tr>
            <td><input type="hidden" name="check_box_1" value="0" />
                <input type="checkbox" name="check_box_1" value="1" />
            </td>
            <td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['title'] ?></a></td>
            <td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['from']; ?></a></td>                
            <td><?php echo $row['created']; ?></td>
        </tr>
        <?php
        }
    }
    ?>
    <input type="submit" value="Delete All" />
    </form>

I want to pass the checkbox through and if the value is 1, process it and delete it. But how would I achieve this with mulitple messages no matter if there is one message or ten? Thanks

3 Answers 3

2

In your form put:

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

Then to process:

if(isset($_POST['check_box_delete']))
{
    foreach($_POST['check_box_delete'] as $id)
    {
        // Delete $id
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Perfect thankyou. I just couldnt work out how to use an array here!
I wouldn't use foreach though. You can to it in a single SQL query.
While you could do it without foreach (or any looping method) it would be very difficult to do all the required auth checking etc. It would be fine to create a single delete query after all checks have been done.
All done, implemented and works a treat. THanks for the tip bud.
0

You can let php store mutliple values in an array by naming the input fields with an array designator suffix []. For example, all checkboxes named checkbox[] will then be stored in $_POST['checkbox'][]. Note that this may not be applicable to checkboxes as their $_POST value only exists if they were checked.

Comments

0
<input type="checkbox" 
       name="mid_to_delete[]" 
       value="some message id"
       id="mid_to_delete_some_message_id">
<label for="mid_to_delete_some_message_id">Delete "Some subject"</label>

and then

<?php
    foreach ($_POST['mid_to_delete'] as $mid) {
        delete_some_message_id($mid);
    }
?>

Since only successful controls are submitted, and unchecked checkboxes are not successful, all the values you get will be ones that have been selected for deletion. (Obviously you still need to perform auth/authz to make sure that the messages being deleted are ones the user is allowed to delete)

Comments

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.