Professional Documents
Culture Documents
</form>
In the PHP script (checkbox-form.php), we can get the submitted option from the
$_POST array. If $_POST['formWheelchair'] is "Yes", then the box was checked. If the
check box was not checked, $_POST['formWheelchair'] won't be set.
<?php
if(isset($_POST['formWheelchair']) &&
$_POST['formWheelchair'] == 'Yes')
else
?>
The value of $_POST['formSubmit'] is set to 'Yes' since the value attribute in the input
tag is 'Yes'.
You can set the value to be a '1' or 'on' instead of 'Yes'. Make sure the check in the PHP
code is also updated accordingly.
There are often situations where a group of related checkboxes are needed on a form.
The advantage of check box group is that the user can select more than one options.
(unlike a radio group where only one option could be selected from a group).
Let's build on the above example and give the user a list of buildings that he is
requesting door access to.
</form>
Please note that the checkboxes have the exact same name ( formDoor[ ] ). Also notice
that each name ends in [ ]. Using the same name indicates that these checkboxes are all
related. Using [ ] indicates that the selected values will be accessed by PHP script as an
array. That is, $_POST['formDoor'] won't return a single string as in the example above;
it will instead return an array consisting of all the values of the checkboxes that were
checked.
$aDoor = $_POST['formDoor'];
if(empty($aDoor))
else
$N = count($aDoor);
?>
If no checkboxes are checked, $_POST['formDoor'] will not be set, so use the "empty"
function to check for this case. If it's not empty, then this example just loops through the
array ( using the "count" function to determine the size of the array ) and prints out the
building codes for the buildings that were checked.
If the check box against 'Acorn Building' is checked, then the array will contain value 'A'.
similarly, if 'Carnegie Complex' is selected, the array will contain C.
It is often required to check whether a particular option is checked out of all the available
items in the checkbox group. Here is the function to do the check:
function IsChecked($chkname,$value)
if(!empty($_POST[$chkname]))
{
foreach($_POST[$chkname] as $chkval)
if($chkval == $value)
return true;
return false;
if(IsChecked('formDoor','A'))
$price += IsChecked('formDoor','A') ? 10 : 0;
$price += IsChecked('formDoor','B') ? 20 : 0;
they are only going to send the values to the server if they are checked.
i know how to post them to another page, but its putting them/sorting them into an
array or sorting through the array
please
Cafu
Student
I found Cafu's post above to be a good jumping off place. $HTTP_POST_VARS is now
deprecated and should be updated to the superglobal $_POST. I updated the code and
tweaked it a bit as I was learning how the code handles checkboxes. My notes are
included in the code.
/* My Logic Is Undeniable */
flann
Novice
Example I have a list of students who were loading a database, and these students are
assigned a rating of another database, as sometimes the ratings are the same selection
is engorosso scores again and again....
case 4:
/ / SCREEN FOR QUALIFYING
print "<TR>";
print "<td> <font face=arial size=3> $ row2 [category] </ font> </ td>";
print "<td> <font face=arial size=3> $ row2 [des_insumo] </ font> </ td>";
print "<td> <font face=arial size=3> $ row2 [drive] </ font> </ td>";
print "<td> <font face=arial size=3>. number_format ($ row2 [precio_unitario],
2,. ,)."</ font> </ td> ";
print "<td> <font face=arial size=3>. number_format ($ row2 [precio_unitario_usd], 2,.
,)."</ font> </ td>";
print "<td> <font face=arial size=3>. number_format ($ row2 [can not], 2,. ,)."</
font> </ td>";
if ($ row2 [branch]! = "none")
(
print "<td> <font face=arial size=3> <A HREF = javascript & #058; popUp
(homologa.php? Id_insumo = $ row 2 [id_insumo])> $ row2 [branch] </ A> </ font>
</ td> ";
)
else
(
$ s1 = "id_insumo = $ row2 [id_insumo] & input = $ row2 [des_insumo]";
print "<td> <font face=arial size=3> <A HREF = javascript & #058; popUp
(homologa.php? id_insumo = $ row 2 [id_insumo])> Index </ A> </ font> </ td> ";
$ homologa_completo = 1;
)
print "</ tr>";
)
mysql_free_result ($ result2);
)
break;
/ / PAGE SETUP
imprime_ajuste_mensual_1 ();
break;
.
Hii there
I had a query regarding capturing multiple checkbox values on the next page. That is if
i have a group of checkboxes with the same name and i select more than one checkbox
how to capture all those values on the next page using PHP. for example consider the
following
how to capture all the checked values on the next page for the checkbox group text
Thanks
Ravi
#2
July 21st, 2004, 04:54 PM
Code:
PHP Code:
foreach( $_POST['text'] AS $checked_value )
{
echo $checked_value
}
..
This is a server side solution and we can get the array value using client side JavaScript we can
also get the value of the array of checkboxes.
Note that if in your server settings global variables is set to OFF then you have to take care for
that.
// This is to collect box array value as global_variables is set off in PHP5 by default
$box=$_POST['box'];
echo "$val,";
<tr bgcolor='#ffffff'>
<td width='25%'> John</td>
<td width='25%'> Mike</td>
<td width='25%'> Rone</td>
</tr>
<tr bgcolor='#f1f1f1'>
<td width='25%'> Mathew</td>
<td width='25%'> Reid</td>
<td width='25%'> Simon</td>
</tr>
value=Select></form></td></tr>
</table>";
When a user fills out options in your form, there might be a list of checkboxes they can tick for
hobbies & interests, it could be useful to store these multiple choices into an array for storing.
<inputtype="checkbox"name="hobbies[]"value="Snooker">Snooker
<inputtype="checkbox"name="hobbies[]"value="Shopping">Shopping
<inputtype="checkbox"name="hobbies[]"value="Fashion">Fashion
<inputtype="checkbox"name="hobbies[]"value="Cinema">Cinema
And once the form is submitted, heres the PHP to process it (for this example, it prints out their
choices):
//useaforeachlooptoreadanddisplayarrayelements
if(is_array($_POST['hobbies'])){
echo'Youselected:<br/>';
foreach($_POST['hobbies']as$a){
echo"<i>$a</i><br/>";
..
Checkboxes are just html. Anybody can code them. Actually, checkboxes belong
somewhere between the open form element, and the close form element. The open
form element should have at least a name,method and action properties and
optionally an enctype property for file/image uploads. Just like this:
</form>
Given that our checkboxes are somewhere between the form open and close,
checkboxes are intended for use when more than one selection is possible. For
example, if you were subscribing to a list of online magazines, then you should
have checkboxes for folks to subscribe to as many magazines as they like. Here are
three fictitious magazines:
Hawaiian Surfer
Boring Accounting
Obviously you would not want to receive the 'Boring Accounting' magazine, so you
would not check that checkbox. If you submit the form - magazineForm.php - the
checkboxes were on you would submit the value of 'on' for the name of the
checkbox you checked. If you did not check the box then the submitted value will
be 'off'. A little code will make this clearer:
Checkbox code
Here's the checkbox code for the above magazines:
$Geek_World_Online_Magazine = !isset($_POST["Geek_World_Online_Magazine"]?
NULL: $_POST["Geek_World_Online_Magazine"];
The effect of this code is to retrieve the value for the variable. Our condition tests
if a variable is set for 'Geek_World_Online_Magazine'. If this variable does not
exist (a set variable with no value would cause our condition to evaluate to true)
then place a value of NULL into $Geek_World_Online_Magazine. Otherwise, grab
the value resulting from a post only.
Ternary operator
The Ternary operator may seem like a veil of obscurity, but after a few years its a
thing of beauty:
$variable = ( condition ) ? "value if condition is true" : "value if condition is
false" ;
Hawaiian Surfer
Boring Accounting
Submit
And there you have seen the value for a checkbox variable, from
the beginning to almost the end.
<table bgcolor="honeydew">
<tr>
<td>
<?php echo"<!-- START of magazineForm.php -->";?>
<form name="magazineForm" method="post"
action="articleCheckbox.php">
<input type="hidden" name="resultsPlease"
value="Right Now!">
<h3>Subscribe to Fictitious Magazines here</h3>
<input type="checkbox"
name="Geek_World_Online_Magazine"<?php if($Geek_World_Online_Magazine
== "on"){echo" CHECKED";}?>> Geek World Online<br>
<input type="checkbox"
name="Hawaiian_Surfer_Magazine"<?php if($Hawaiian_Surfer_Magazine == "
on"){echo" CHECKED";}?>> Hawaiian Surfer<br>
<input type="checkbox"
name="Boring_Accounting_Magazine"<?php if($Boring_Accounting_Magazine
== "on"){echo" CHECKED";}?>> Boring Accounting<br>
<input type="submit" name="submit" value="Submit">
</form>
<a href="#source">Jump to source code section</a><br>
<?php echo"<!-- END of magazineForm.php -->";?>
</td>
</tr>
</table>
... then you would test for the value (after capturing the value),
like:
That's it for one checkbox. Add as many more as you like, but be
sure to keep the property called name different for each checkbox
you have.