0

I have created a populated dropdown list in HTML. When the two dropdowns have been selected, the form posts to 'calculate.php' where the php script echoes out the selected values from the dropdowns.

<form action="calculate.php" method='POST'>
<select name="Phones">
<option value="Lumia800">Nokia Lumia 800</option>
<option value="iPhone">Apple iPhone 4s</option>
<option value="GalaxyS2">Samsung Galaxy S2</option>
<option value="Bold9900">Blackberry Bold 9900</option>
<option value="SensationXE">HTC Sensation XE</option>
<option value="XperiaS">Sony Ericsson Xperia S</option>
</select>

<select name="Network">
<option value="Orange">Orange</option>
<option value="Vodafone">Vodafone</option>
<option value="O2">O2</option>
<option value="Three">Three</option>
</select>

<input type="submit" />
</form>

In this one example, 'calculate.php' echoes the selected dropdown values.

<?php 
$pref=$_POST['Phones'];
$pref1=$_POST['Network'];

$Orange="5";

if ($pref1 == "Orange")
{
    $total1 = $Orange;
}

$Lumia800="10";

if ($pref == "Lumia800")
{
    $total = $Lumia800 + $Orange;
}

echo $total;

    ?>

This example output is '15'

The aim of this example is to eventually create a simple phone package system, with this set of code adding the price of the phone, the network costs, and other variables together for a final total of how much it would cost when the user submits it.

The problem I have is that I want to add each variable from the corresponding dropdown boxes ("Phones" and "Network" for example) against each other. Is my example going to be too over complicated for such a simple script? Is there a more refined method to get the results that I want?

Thanks,

JB.

1
  • I don't think using if statements for each possible combination is going to be easy to maintain down the track. Perhaps a better way might be to store the values in an array or even a database, get the values out and perform your addition on the fly. Commented Mar 29, 2012 at 7:59

5 Answers 5

1

You should define arrays for each select that contains all prices.

$phone_prices = array("Lumia800" => 10, "iPhone" => 42);
$network_prices = array("Orange" => 5, ...);

And then you coud simply do something like

$price = $phone_prices[$pref] + $network_prices[$pref1];

But this is not really safe, make sure $pref and $pref1 are existing indexes in their respective array.

Sign up to request clarification or add additional context in comments.

Comments

0

It will work but might be complicated. I would suggest changing over to a switch statement instead.

$total = 0
switch ($pref) {
  case "Orange":
    $total +=  5;
    break;
  case "O2":
    $total +=  8;
    break;
}

Same switch structure would be for the $pref1 or network variable.

Comments

0
<form action="calculate.php" method='POST'>
  <select name="Phones">
   <option value="123">Nokia Lumia 800</option>
   <option value="234">Apple iPhone 4s</option>
   <option value="12">Samsung Galaxy S2</option>
   <option value="32">Blackberry Bold 9900</option>
   <option value="432">HTC Sensation XE</option>
   <option value="12">Sony Ericsson Xperia S</option>
  </select>

  <select name="Network">
   <option value="42">Orange</option>
   <option value="34">Vodafone</option>
   <option value="23">O2</option>
   <option value="34">Three</option>
  </select>

  <input type="submit" />
</form>

and

<?php
 echo $_POST['Network'] + $_POST['Phones'];
?>

might be a lot simpler in that case, but that of course depends on the exact situation where you need this.

2 Comments

The flaw with this approach would be that if the values are added together and then sent to some payment processing system. In this case, the user can easily edit his POST variables and cause problems. :P However, if it is to be used for just displaying a price to the user, I don't think there should be too much an issue :)
Yep, that's why I directly added that it depends on the exact situation, but if it's for a payment processing system it would output ids from a database rather than custom titles and the match to these id's would either way have to happen server side.
0

G'day mate.

You could for one, create an array of mapping from Phones => Price and Network => Price

example:

<?php
$phones = array(
   'Lumia800' => 10,
   'iPhone' => 15,
   'GalaxyS2' => 30,
   // rest of the phones with price
);

$networks = array(
   'Orange' => 10, 
   'Vodafone' => 15,
   'Three' => 3,
   'O2' => 8
);

then validate user input

if(!empty($phones[$pref])) && !empty($networks[$pref1])) {
    echo $phones[$pref] + $networks[$pref1];
}

Comments

0

If your code isn't going to be more complicated then what i see now I would just change:

...
 <option value="iPhone">Apple iPhone 4s</option>
...
<option value="Orange">Orange</option>
...

to

...
<option value="5">Apple iPhone 4s</option>
...
<option value="10">Orange</option>
...

And then loop all the values at the end

$total = 0;

foreach ($_POST as $key => $val) {
    $total += $val;
}

echo $total;

That way you can change the amount of dropdowns

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.