In the php Tutorial, the checkbox value is obtained and displayed. Multiple values are obtained.
Simplest checkbox value code
<Html>
<Head>
<Title> checkbox demo </title>
</Head>
<Body>
<H1> checkbox demo
<H3> demonstrates checkboxes <Form action = "handleformcheckbox. php">
<Ul>
<Li> <input type = "checkbox" name = "chkfries" value = "11.00"> fries </li>
<Li> <input type = "checkbox" name = "chksoda" value = "12.85"> soda </li>
<Li> <input type = "checkbox" name = "chkshake" value = "1.30"> shake </li>
<Li> <input type = "checkbox" name = "chkketchup" value = ". 05"> ketchup </li>
</Ul>
<Input type = "submit">
</Form>
</Body>
</Html>
<! -- Handleformcheckbox. php
<Html>
<Head>
<Title> checkbox demo </title>
</Head>
<Body>
<H3> demonstrates reading checkboxes <?
Print Chkfries: $ chkfries <br>
Chksoda: $ chksoda <br>
Chkshake: $ chkshake <br>
Chkketchup: $ chkketchup <br>
<Hr>
Here;
$ Total = 0;
If (! Empty ($ chkfries )){
Print ("you chose fries <br> ");
$ Total = $ total + $ chkfries;
}
If (! Empty ($ chksoda )){
Print ("you chose soda <br> ");
$ Total = $ total + $ chksoda;
}
If (! Empty ($ chkshake )){
Print ("you chose shake <br> ");
$ Total = $ total + $ chkshake;
}
If (! Empty ($ chkketchup )){
Print ("you chose ketchup <br> ");
$ Total = $ total + $ chkketchup;
}
Print "the total cost is $ total ";
?>
</Body>
</Html>
-->
Instance
<Html>
<Head>
<Title> using default checkbox values </title>
</Head>
<Body>
<? Php
$ Food = $ _ get [food];
$ Self = htmlentities ($ _ server ['php _ self ']);
If (! Empty ($ food )){
Echo "the foods selected are: <br/> ";
Foreach ($ food as $ foodstuf)
{
Echo "<strong>". htmlentities ($ foodstuf). "</strong> <br/> ";
}
}
Else
{
Echo ("<form action =" $ self "");
Echo ('method = "get">
<Fieldset>
<Label> italian <input type = "checkbox" name = "food []" value = "italian"/>
</Label>
<Label> mexican <input type = "checkbox" name = "food []" value = "mexican"/>
</Label>
<Label> chinese <input type = "checkbox" name = "food []" value = "chinese"
Checked = "checked"/> </label>
</Fieldset>
<Input type = "submit" value = "go! "> ');
}
?>
</Body>
</Html>
Multiple choice checkbox
<? Php
$ Options = array ('option 1', 'option 2', 'option 3 ');
$ Valid = true;
If (is_array ($ _ get ['input']) {
$ Valid = true;
Foreach ($ _ get ['input'] as $ input ){
If (! In_array ($ input, $ options )){
$ Valid = false;
}
}
If ($ valid ){
// Process input
}
}
?>
Instance checkbox multi-value acquisition
<Html>
<Head>
<Title> using default checkbox values </title>
</Head>
<Body>
<? Php
$ Food = $ _ get ["food"];
If (! Empty ($ food )){
Echo "the foods selected are: <strong> ";
Foreach ($ food as $ foodstuff ){
Echo '<br/> '.html entities ($ foodstuff );
}
Echo "</strong> .";
}
Else {
Echo ('
<Form action = "'. htmlentities ($ _ server [" php_self "]).'" method = "get">
<Fieldset>
<Label>
Italian
<Input type = "checkbox" name = "food []" value = "italian"/>
</Label>
<Label>
Mexican
<Input type = "checkbox" name = "food []" value = "mexican"/>
</Label>
<Label>
Chinese
<Input type = "checkbox" name = "food []" value = "chinese" checked = "checked"/>
</Label>
</Fieldset>
<Input type = "submit" value = "go! "/>
</Form> ');
}
?>
</Body>
</Html>