checkboxes and php post

They have: 426 posts

Joined: Feb 2005

I want to add some check boxes next to my pages in the administration area which lists all the pages i have in the system. When checked and posted these pages can then be deleted. But how can i deal with multiple checks if i name the checkboxes the same name? For example:

<input type="checkbox" name="check" value="{$Page_ID}">check1
<input type="checkbox" name="check" value="{$Page_ID}">check2
<input type="checkbox" name="check" value="{$Page_ID}">check3
<input type="checkbox" name="check" value="{$Page_ID}">check4
'

if i post and pick up $_POST['check']

if there a way i can extract the page_IDs in a loop and delete from the database. Perhaps in a while loop or something:

<?php
while($_POST['checkbox']){
    
mysql_query(\"delete from db where Page_ID={$_POST['checkbox']}\",$con);
}
?>

OR something like this?

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Sure can! Use an array for the names. They will all have to be different. The value will all be 1 (which will evaluate to "true" when we check if they are checked). The array indexes do not have to be in numerical order or not even numbers, it can be something like check['something'].

HTML Form:

<input type="checkbox" name="check[1]" value="1">check1
<input type="checkbox" name="check[2]" value="1">check2
<input type="checkbox" name="check[3]" value="1">check3
<input type="checkbox" name="check[4]" value="1">check4

In the PHP loop, $field is the index of the array, and $value is 1 if checked, and nothing if not checked.

PHP Loop:

<?php
  
foreach ($_POST['check'] as $field => $value)
   {
      if (
$value)
      {
        
/* the checkbox was checked */
        
         /* TODO: code to delete stuff */
     
}
   }
?>

Hope this helps!

They have: 426 posts

Joined: Feb 2005

Are yes. Thanks, was almost there?"!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.