How to change a number based on what someone chooses?

Hi Everyone,

In the below URL if a person chooses “whatever” or “whatever2”, I would like to get the price to adjust upwards or downwards based on what they choose. My syntax is below. I’m getting a silly error but I can’t seem to fix what I’m doing wrong. Any help would be great.

Thanks!

http://whatsmyowncarworth.com/auto-members/math/math.php

<?php

//Connect to the database through our include
include "connect_to_mysql.php";

// Query member data from the database and ready it for display
$sql = mysql_query("SELECT price FROM car_data2");
while($row = mysql_fetch_array($sql)){

$price = $row["price"];

}

?>

<?php
  error_reporting(E_ALL | E_STRICT);
  ini_set("display_errors", 1);
?>

<?php

$priceMods = array ('whatever' => 100, 'whatever2' => -100);

if (isset ($priceMods[$_POST['choose']]) {

    $price += $priceMods[$_POST['choose']];
	
}

?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<?php echo "$price"; ?>
<form action="math.php" method="post" enctype="multipart/form-data" name="form" id="form" onSubmit="return validate_form ( );">
  <table>
    <tr>
      <td>Choose:</td>
      <td>
        <select name="choose">
          <option value="whatever">Whatever</option>
          <option value="whatever2">Whatever2</option>
        </select>
      </td>
    </tr>
    <tr>
      <td width="99">
        <input type="submit" name="submit" value="Contact Us">
      </td>
    </tr>
  </table>
</form>
</body>
</html>

Check all opening and closing brackets in this line:

if (isset ($priceMods[$_POST['choose']]) { 

By the way, you should this on top of your script, not in the middle:

<?php
  error_reporting(E_ALL | E_STRICT);
  ini_set("display_errors", 1);
?> 

And the result of this loop will be only the last price:

while($row = mysql_fetch_array($sql)){

$price = $row["price"];

} 

If you want all prices, you should use $price so all prices will be stored in an array.

And a last advice: since the mysql_ database extension is becoming deprecated, you might want to take a look at [URL=“http://www.php.net/manual/en/book.mysqli.php”]mysqli or [URL=“http://www.php.net/manual/en/book.pdo.php”]pdo

Hi,

Thanks for your information. It’s working great!

I just have one minor question. The original price is 7895.00. If you click on “contact us” and choose the “whatever” options the prices (depending on what option you choose) will decrease. My question is, is their anyway to get back to the original price of 7895.00? How can they choose that?

Thanks again for everyone’s help. Much appreciated.

<?php
 /* error_reporting(E_ALL | E_STRICT);
  ini_set("display_errors", 1); */
?>


<?php

//Connect to the database through our include
include "connect_to_mysql.php";

// Query member data from the database and ready it for display
$sql = mysql_query("SELECT price FROM car_data2");
while($row = mysql_fetch_array($sql)){

$price = $row["price"];

}

?>


<?php

$priceMods = array ('whatever' => -100, 'whatever2' => -200, 'whatever3' => -300, 'whatever4' => -400);

if (isset ($priceMods[$_POST['choose']])) {

    $price += $priceMods[$_POST['choose']];
	
}

?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<?php echo "$price"; ?>
<form action="math.php" method="post" enctype="multipart/form-data" name="form" id="form" onSubmit="return validate_form ( );">
  <table>
    <tr>
      <td>Choose:</td>
      <td>
        <select name="choose">
          <option value="whatever">Whatever</option>
          <option value="whatever2">Whatever2</option>
		  <option value="whatever3">Whatever3</option>
		  <option value="whatever4">Whatever4</option>
        </select>
      </td>
    </tr>
    <tr>
      <td width="99">
        <input type="submit" name="submit" value="Contact Us">
      </td>
    </tr>
  </table>
</form>
</body>
</html>

Add an option with discount 0 ?

Thanks guido2004. The code is working great. Appreciate the help!