开发者

Checkbox in a while loop

开发者 https://www.devze.com 2023-01-28 19:52 出处:网络
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 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


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
    }
}


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.


<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)

0

精彩评论

暂无评论...
验证码 换一张
取 消