/    Sign up×
Community /Pin to ProfileBookmark

How to get the sum of selected checkboxes?

How to get the sum of selected checkboxes?

plz help

[code=php]

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
</head>

<body>

<?php
// Code downloaded from html-form-guide.com
// This code may be used and distributed freely without any charge.
//
// Disclaimer
// ———-
// This file is provided “as is” with no expressed or implied warranty.
// The author accepts no liability if it causes any damage whatsoever.
//

if(isset($_POST[‘formSubmit’]))
{
$aDoor = $_POST[‘formDoor’];

if(empty($aDoor))
{
echo(“<p>not selected</p>n”);
}
else
{
$N = count($aDoor);

echo(“<p>you select $N box <br>”);
for($i=0; $i < $N; $i++)
{
echo($aDoor[$i] . ” “);
}

echo(“</p>”);
}

//and so on
}

?>

<form action=”<?php echo htmlentities($_SERVER[‘PHP_SELF’]); ?>” method=”post”>
<p>
Which buildings do you want access to?<br/>
<input type=”checkbox” alt=”gio” name=”formDoor[]” value=”10″ />Acorn Building<br />
<input type=”checkbox” name=”formDoor[]” value=”20″ />Brown Hall<br />
<input type=”checkbox” name=”formDoor[]” value=”30″ />Carnegie Complex<br />
<input type=”checkbox” name=”formDoor[]” value=”40″ />Drake Commons<br />
<input type=”checkbox” name=”formDoor[]” value=”50″ />Elliot House
</p>

<input type=”submit” name=”formSubmit” value=”Submit” />
</form>

</body>
</html>

[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@kbduvallMay 31.2012 — [code=php]
<?php
$boxesChecked = count($_POST['formDoor']);

$totalValue = 0;
foreach ($_POST['formDoor'] as $val)
{
$totalValue += $val;
}

echo "Total Boxes Checked: {$boxesChecked}<br />n";
echo "Sum of Checked Boxes' Values: {$totalValue}<br />n";
?>
[/code]
×

Success!

Help @redeagle1191 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...