Forum Moderators: coopster
Now my problem is i want to assign a price for every product and then let the script add the price of the products selected! and show the total price!
<input type="radio" name="product1" value="product a"/>
<input type="radio" name="product1" value="product b" />
<input type="radio" name="product1" value="product c" />
the value i have is for the product name to appear in the next page, now how could i add price for every product and then add them to give a total price.
thanks in advance!
You can put the product names and prices into an array. With many languages, an array index has to be a number. php does its arrays a little differently and allows you more freedom with your indexes:
$prices['product a'] = 15;
That line declares $prices to be an array and the element indexed by product a is set to 15. To set more than one at a time, here's the syntax:
$prices = array("product a" => 15, "product b" => 20, "product c" => 30);
Now if you were to echo one:
echo $prices['product b'];
You'd get 20.
Now let's say that someone selected product b on your form. So if you're using method="post" on your form you now have:
$_POST['product1'] = 'product b'
You can do this:
echo $prices[$_POST['product1']]
and you'll still get 20. So,
$total = $prices[$_POST['product1']] + $prices[$_POST['product2']] + $prices[$_POST['product3']] + $prices[$_POST['product4']];
A much more thorough explanation of arrays:
[php.net ]