I have a form built with checkboxes but I am unable to calculate the total result for it. I want to get the total if user selects three checkboxes total will be $3 if one then $1. I am stuck as I am unable to do the calculation.
<html
<head>
<title>Order</title>
</head>
<body>
<link rel= "stylesheet" href= "order.css">
<form action="complete.php" method="post">
<form name="order">
<fieldset><legend>Complete Order:</legend>
<h1> Choose Design </h1>
<p><label>Your Name: <input type="text" name="name"></label>
<label>Address: <input type="text" name="address"></label>
<label>Credit Card #: <input type="text" name="creditcard"></label>
<label>Date: <input type="date" id="datepicker" name='date' size='9' value="" > </label>
<br><label> Design Types: <img src="1.jpg"><input type="checkbox" name="1"></label> $1.00
<label><img src="2.jpg"><input type="checkbox" name="2"> </label>$1.00
<label><img src="3.jpg"><input type="checkbox" name="3"> </label>$1.00
<br></p>
<input type="submit" value="Submit Order">
</form>
</body>
</html>
Php code
<html>
<head>
<title>Form Feedback</title>
</head>
<body>
<?php
if ( !empty($_POST['name']) && !empty($_POST['address']) && !empty($_POST['creditcard']) ) {
echo "<p>Thank you, <strong>{$_POST['name']}</strong>, for placing the order.
<p>Your item will be shipped to:
<tt>{$_POST['address']}</tt></p>
<p>Following credit card has been charged: <em>{$_POST['creditcard']}</em>.</p>
";
} else { //Missing form value.
echo '<p>Please go back and fill out the form again.</p>';
}
?>
</body>
</html>
Changes in HTML:
<label><img src="1.jpg"><input type="checkbox" name="fieldname[]" value="1"></label> $1.00
<label><img src="2.jpg"><input type="checkbox" name="fieldname[]" value="1"> </label>$1.00
<label><img src="3.jpg"><input type="checkbox" name="fieldname[]" value="1"> </label>$1.00
PHP Code: it will count number of selected checkboxes-
<?php
if(isset($_POST['fieldname']) && !empty($_POST['fieldname'])){
echo count($_POST['fieldname']); //count the number of selected checkbox
echo array_sum($_POST['fieldname']); //sum of selected checkbox values
}
?>
First of all, it's about checkbox's name. If they are a group of values (e.g: products), use a common name as an array:
<input type="checkbox" name="product[]" />
<input type="checkbox" name="product[]" />
<input type="checkbox" name="product[]" />
The second question, shouldn't your checkbox has a value or it'll be always $ 1.00?
It it'll be always $1.00, just count it:
$value = isset($_POST['product']) ? count($_POST['product']) : 0;
If it should has a value:
<input type="checkbox" name="product[]" value="1.00" />
<input type="checkbox" name="product[]" value="1.50" />
<input type="checkbox" name="product[]" value="2.00" />
Can use the array_sum function:
$value = isset($_POST['product']) ? array_sum($_POST['product']) : 0;
Use isset($_POST['checkboxName'])
to check if the checkbox is checked and then add 1 for each checkbox:
$check_value = 0;
$check_value += isset($_POST['1']) ? 1 : 0;
$check_value += isset($_POST['2']) ? 1 : 0;
$check_value += isset($_POST['3']) ? 1 : 0;
Change the php part:
<?php
if ( !empty($_POST['name']) && !empty($_POST['address']) && !empty($_POST['creditcard']) ) {
$cost = 0;
if(isset($_POST['1'])) $cost += 1;
if(isset($_POST['2'])) $cost += 1;
if(isset($_POST['3'])) $cost += 1;
echo "<p>Thank you, <strong>{$_POST['name']}</strong>, for placing the order.
<p>Your item will be shipped to:
<tt>{$_POST['address']}</tt></p>
<p>Following credit card has been charged: <em>{$_POST['creditcard']}</em>.</p>
<p>With amount: <em>{$cost}</em>.</p>
";
} else { //Missing form value.
echo '<p>Please go back and fill out the form again.</p>';
}
?>