Page 1 of 1
Check Boxes
Posted: Wed Jun 12, 2002 2:07 pm
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
Posted: Wed Jun 12, 2002 2:32 pm
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ї'mycheckboxname'])
{
echo 'you checked the box';
}
else
{
echo 'you did not check the box';
}
?>
<form action="" method="post" name="form">
<input type="checkbox" name="check" checked><br>
<input type="submit" name="submit">
</form>
hope this helps
Posted: Wed Jun 12, 2002 2:59 pm
by Pintonite
I understand that but how do I do it dynamically
Posted: Thu Jun 13, 2002 7:05 am
by mikeq
Do what dynamically, create them or check for them being set??
Posted: Thu Jun 13, 2002 11:33 am
by Pintonite
Ahh nevermind. I'm gonna take another approach to it. Thanks for the help.
Posted: Thu Jun 13, 2002 7:22 pm
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.