Get the PHP code for the checkbox value

Source: Internet
Author: User
Tags foreach php code php tutorial

Because of the checkbox specificity, so we have to get its value, in my language is not the same, in the PHP tutorial we are in the form of an array, and the use of traversing his value, there are a number of examples below.

<title>checkbox demo</title>
<body>

<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 = ". >ketchup</li>"
</ul>
<input type = "Submit" >
</form>

</body>


<!--handleformcheckbox.php
<title>checkbox demo</title>
<body>
?
Print <<Chkfries: $chkfries <br>
Chksoda: $chksoda <br>
Chkshake: $chkshake <br>
Chkketchup: $chkketchup <br>

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>


-->

Instance

<title>using Default checkbox values</title>
<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>


Multiple Select 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 Fetch

<title>using Default checkbox values</title>
<body>
<?php
$food = $_get["Food"];//www.3ppt.com
if (!empty ($food)) {
echo "The foods selected are: <strong>";
foreach ($food as $foodstuff) {
echo ' <br/> '. htmlentities ($foodstuff);
}
echo "&LT;/STRONG&GT;.";
}
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>

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.