Php checkbox value acquisition, display, multiple values acquisition

Source: Internet
Author: User
Tags foreach php tutorial

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>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.