How to change a number based on what someone chooses?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcc_shane
Forum Newbie
Posts: 22
Joined: Sat May 12, 2012 1:47 pm

How to change a number based on what someone chooses?

Post by mcc_shane »

In the below URL if a person chooses "whatever" or "whatever2" how would I get the price to adjust upwards or downwards based on what they choose? Would that be related to a mysql statement? Thanks man!

For example, if they choose "whatever" the price goes up for 100 or "whatever2" the price goes down by 100

I added a if statement (below) and I can't seem to get it working. I've been playing around with this the past few hours and can't seem to get it working. Any pointers? Thanks everyone!

http://whatsmyowncarworth.com/auto-memb ... h/math.php

Code: Select all

<?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 * FROM car_data2");
while($row = mysql_fetch_array($sql)){

$price = $row["price"];

}

?>

<?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>
<p>&nbsp;</p>
</body>
</html>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to change a number based on what someone chooses?

Post by social_experiment »

Change the values within the drop down menu

Code: Select all

<select name="choose">
          <option value="100">100 More</option>
          <option value="-100">100 Less</option>
        </select>
You can now remove the array

Code: Select all

<?php
 if (isset ($_POST['choose'])) {
    $price += $_POST['choose'];
}
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply