Check Boxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pintonite
Forum Newbie
Posts: 17
Joined: Mon May 20, 2002 5:11 pm
Contact:

Check Boxes

Post by Pintonite »

Can someone give me an explanation on how to use Check Box's with PHP. Is it a switch? An Array? I am trying to have it so a user can delete a user specified number of items
Zmodem
Forum Commoner
Posts: 84
Joined: Thu Apr 18, 2002 3:59 pm

Post by Zmodem »

It's neither a switch nor an array.

The checkbox HTML looks like this:

<input type="checkbox" name="mycheckboxname">

Now, when this checkbox is submitted via a form to a PHP script, one of two things will happen:

1) If the box IS checked, then the variable $mycheckboxname will exist. You can check for it's existance by using isset($mycheckboxname)

2) If the box has NOT been checked, then the variable $mycheckboxname will not exist at all.

Here is some code:

Code: Select all

<?php
   if (isset($_POST&#1111;'mycheckboxname'])
   &#123;
      echo 'you checked the box';
   &#125;
   else
   &#123;
      echo 'you did not check the box';
   &#125;
?>

<form action="" method="post" name="form">
   <input type="checkbox" name="check" checked><br>
   <input type="submit" name="submit">
</form>
hope this helps
Pintonite
Forum Newbie
Posts: 17
Joined: Mon May 20, 2002 5:11 pm
Contact:

Post by Pintonite »

I understand that but how do I do it dynamically
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Do what dynamically, create them or check for them being set??
Pintonite
Forum Newbie
Posts: 17
Joined: Mon May 20, 2002 5:11 pm
Contact:

Post by Pintonite »

Ahh nevermind. I'm gonna take another approach to it. Thanks for the help.
User avatar
roninblade
Forum Newbie
Posts: 21
Joined: Thu Jun 13, 2002 7:12 pm

Post by roninblade »

you shouldnt quit on it just like that. using checkboxes in php is very easy, here's an example:

create your checkboxes like this <input type="checkbox" name="tobedeleted[]">

then delete the items like this...

Code: Select all

<?php
$delete_string = implode(",", $tobedeleted);

$query = "delete from $your_table where $your_field IN($delete_string)";
@mysql_query($query);
?>
i assumed there that you were using a database. i hope that helps.
Post Reply