Forum Moderators: coopster
I'm creating an order form whereby result is displayed in the next page.
<input type="radio" name="gift" value="toy A"/>
<input type="radio" name="gift" value="toy B"/>
<input type="radio" name="gift" value="toy C"/>
They cost $5, $10 and $15 respectively.
<input type="checkbox" name="extras[]" value="Ribbon" />
<input type="checkbox" name="extras[]" value="Card" />
<input type="checkbox" name="extras[]" value="Wrapper" />
<input type="checkbox" name="extras[]" value="Box" />
Additional $1 for each of the extras checked.
How can I know which toy is chosen, which extras are selected and total up the cost? I just need to display the final total cost.
Will appreciate any help. Thanks!
Your additional items are stored in the $_POST['extras'] array, so to get the cost you can count the slots the array has when the form is submitted. If there are none checked, the count will be 0.
if (!empty($_POST['extras']))
{
echo count [uk.php.net]($_POST['extras']);
}
To display what was checked, implode [uk.php.net] the array.
So, like this:
if (!empty($_POST['extras']))
{
echo "You have chosen <b>".implode(",",$_POST['extras'])."</b> at an additional cost of $".count($_POST['extras']).";
}
Hope that helps.
dc
$price = array('toy A'=>5, 'toy B'=>10, 'toy C'=>15, 'extra'=>1);
if(in_array [php.net]($_POST['gift'], $price))
$cost = $price[$_POST['gif']];
else $error = "Wrong toy, or whatever error you like";
if (!empty($_POST['extras']))
{
$cost += count($_POST['extras']) * $price['extra'];
}
Regards
Michal
$price = array('toy A'=>5, 'toy B'=>10, 'toy C'=>15, 'extra'=>1);if(in_array($_POST['gift'], $price))
$cost = $price[$_POST['gift']];
else $error = "Wrong toy, or whatever error you like";
if (!empty($_POST['extras']))
{
$cost += count($_POST['extras']) * $price['extra'];
}
I think that $$ was a spelling mistake, cause he got something out of it.
in_array() checks the values, not the keys. So this line:
if(in_array($_POST['gift'], $price))
should be:
if(array_key_exists($_POST['gift'],$price))