March 5, 2012
PHP Forms – Numeric Value of Checkbox
Question by Patrick Appelman
I am creating an online order form for multiple products. I need to calculate the total cost for the products selected via checkbox and send it as a confirmation e-mail. The value of the checkbox is the price in dollars.
<input type="checkbox" id="product1" name="product1" value="100" />
<input type="checkbox" id="product2" name="product2" value="250" />
In my ‘process.php’ file, I need to total the cost for all items if they are checked.
if(isset($_POST['product1']) && $_POST['product1'] == '100') {
$product1 = 100;
}
if(isset($_POST['product2']) && $_POST['product2'] == '250') {
$product2 = 250;
}
$dollars = $product1 + $product2;
When I try to do it this way, $dollars is an empty variable “”. Can someone tell me how to fix this?
Thank you!
Answer by Starx
There is no syntactical error in your code. So the only explanation is that,
- $_POST[‘product1’] does not have value
100
or they are not sent through post at all - $_POST[‘product2’] also does not have value
250
or they are not sent through post as well
In order to verify this, do a quick var_dump($_POST)
at the top of your .php file