Page 1 of 1

help calculating total from option boxes

Posted: Mon Oct 20, 2008 4:42 am
by dthomas31uk
I have 3 option boxes

Code: Select all

echo 'Load from 14m3 - 20m3' . "<label><input type='radio' name='load' value='luton'</label>"."<BR>";
 
echo 'Load from 7m3 - 14m3'."<label><input type='radio' name='load' value='full_load'</label>"."<BR>";
 
echo 'Load from 1m3 - 7m3' . "<label><input type='radio' name='load' value='half_load'</label>"."<BR>"; 
 
And need to calculate totals based on one of the options selected. Trouble is I think my if statements are not written correctly. Can anyone advise here is the code that calcultes the option boxes from my database

Code: Select all

if ($load == 'full_load') {
 
  $total = $row['full_price'];  
 
} 
 
if  ($load == 'half_price') {
 
  $total = $row['half_price'];
 
}
 
if  ($load == 'luton') {
 
  $total = $row['luton'];
 
} 
 

Re: help calculating total from option boxes

Posted: Mon Oct 20, 2008 4:48 am
by papa
You could use a switch:

Code: Select all

 
$load = $_POST['load'] //Depending on your form method
 
switch($load) {
 case "full_load":
$total = $row['full_price'];  
 break;
 case "half_price":
  $total = $row['half_price'];
 break;
 case "luton":
$total = $row['luton'];
 break;
}
 

Re: help calculating total from option boxes

Posted: Mon Oct 20, 2008 4:58 am
by dthomas31uk
Hi papa. had it working when I had two option buttons like so

Code: Select all

if ($load == 'full_load') {
  $total = $row['full_price'];
  
} else {
  $total = $row['half_price'];
}
 
have tried your switch code, but it only calculates the 'half_price' , dont know why

Re: help calculating total from option boxes

Posted: Mon Oct 20, 2008 5:19 am
by aceconcepts
You haven't ended your input elements:

Change

Code: Select all

<input type='radio' name='load' value='half_load'
 
To

Code: Select all

<input type='radio' name='load' value='half_load' />
Forgot to mention, you need to do this to all of your radio elements.

Re: help calculating total from option boxes

Posted: Mon Oct 20, 2008 5:37 am
by dthomas31uk
have got it working now Papa cheers. The problem was that I did not select my third option in my select query, thus it did not calculate. Thanks a lot

Re: help calculating total from option boxes

Posted: Mon Oct 20, 2008 5:38 am
by papa
No probs. Good it worked out for you!