FRIHOSTFORUMSSEARCHFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

How to Reference Checkbox Selections?

 


Dennise
I know how to reference radio button selections, text-box contents and drop-down list selections. I mostly do this using the POST method for form processing.

But I haven't figured out how to reference selections made in a group of check boxes! This can't be difficult.

Can someone please point me in the right direction Rolling Eyes
guitarcrazy087
This is pretty simple actually. If the check box is checked, the value associated with the check box name will be 1. If it's not checked, it will be 0. It's just a Boolean value I believe... I think that's what it's called...

Hope you get it worked out!
sonam
You need to use different names and values for each checkbox. Then when you submit your form (using POST metod) you will get an array of checked checkboxes. Here is simple example and PHP then you can see result when you check some box:
HTML:
Code:
<form name="form1" method="post" action="process.php">
     <input type="checkbox" name="checkbox1" value="Mercedes" />
     <input type="checkbox" name="checkbox2" value="Ford" />
     <input type="checkbox" name="checkbox3" value="Volvo" />
     <input type="checkbox" name="checkbox4" value="Fiat" />
     <input type="submit" name="Submit" value="Submit" />
</form>


PHP (process.php):
Code:
<?php
print_r($_POST);
?>
Peterssidan
You can use the same name for all the checkboxes ending with []. In this example we use the name "cars[]":
Code:
<input type=checkbox name="cars[]" value="Mercedes"  />
<input type=checkbox name="cars[]" value="Ford"  />
<input type=checkbox name="cars[]" value="Volvo"  />
<input type=checkbox name="cars[]" value="Fiat"  />


In php the data recieved can now be handled like an array with the name $_POST['cars']. The following code will print all cars selected:
Code:
foreach($_POST['cars'] as $car)
{
   echo "$car<br />"; // or do something more useful
}
sonam
Peterssidan wrote:
You can use the same name for all the checkboxes ending with []. In this example we use the name "cars[]":
Code:
<input type=checkbox name="cars[]" value="Mercedes"  />
<input type=checkbox name="cars[]" value="Ford"  />
<input type=checkbox name="cars[]" value="Volvo"  />
<input type=checkbox name="cars[]" value="Fiat"  />


In php the data recieved can now be handled like an array with the name $_POST['cars']. The following code will print all cars selected:
Code:
foreach($_POST['cars'] as $car)
{
   echo "$car<br />"; // or do something more useful
}


I know PHP can handle this but did you check is this valid XHTML. I am asking this stupid question because I have problem vith validation ones when I use the same names in XHTML. Then I delete names (because I don't need it) and validation is OK. How XHTML read names cars[] in inputs like different or like the same?

Sonam
Peterssidan
sonam wrote:
I know PHP can handle this but did you check is this valid XHTML. I am asking this stupid question because I have problem vith validation ones when I use the same names in XHTML. Then I delete names (because I don't need it) and validation is OK. How XHTML read names cars[] in inputs like different or like the same?


The only errors I find when validating my code with W3C Markup Validator is that type=checkbox should be type="checkbox":
Code:
<input type="checkbox" name="cars[]" value="Mercedes"  />
<input type="checkbox" name="cars[]" value="Ford"  />
<input type="checkbox" name="cars[]" value="Volvo"  />
<input type="checkbox" name="cars[]" value="Fiat"  />


I think XHTML read them as the same name and everything has to do with the way PHP handles it, but I'm not sure.
sonam
I was check one other way and get valid results Dancing If I think better I got this error because PHP write (in foreach loop) the same form name not the same name for inputs.

Sonam
Dennise
Hey All,

Thanks for the replies.

For Form Submits, this works for me too:

<?php
$checkbox1 = $_POST['cbx1'];
$checkbox2 = $_POST['cbx2'];
$checkbox3 = $_POST['cbx3'];
echo "Checked choices are: "; echo $checkbox1; echo $checkbox2; echo $checkbox3;
?>
Very Happy
kacsababa
Yeah, thats the basic situation. When php recieves a POST with multiple parameters it process it as an array, where every sent input is an item with the name is the key and the value is the value.
But what Peterssidan said is a step forward. With the "cars[]" name you put a sub-array into the POST array, so you can loop through just the corresponding input values (eg the checkboxes).
Related topics

Extraer los canales de color en una imagen a color
Tutorial: PHP Installed Modules Dynamic Reference Tool
Review: GNOME 2.14 (from Linux.com)
Another religions
how to design this

I don't get Javascript.
reference for enabling multimedia in Linux
Add HTML signatures with images to ur email...GUIDE
PHP Form Question
help getting form info into mysql db

ned help to delete the selected checkbox
Earliest reference to Christ reported to be found
Javascript Function Reference
De da vinci code
W3C buttons without images
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.