Jump to content

Calculator form help


mgoblue4

Recommended Posts

Hello, I am fairly new to PHP to please be easy on me :)

I am having trouble with a calculator form, currently it is not displaying the result after you click the submit button.

http://stopsmokingecig.com/smoking-calc.php

 

Here is the code....

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">

<input type="text" name="packs" value="" />How many packs a week do you smoke?<br>
<input type="text" name="price" value="" />How much do you pay per pack?<br>
<input type="text" name="months" value="" />Over how many months?<br>
<?php
echo "$number";
?>

<input type="submit" name="multiply" value="Calculate" />
</form>

<?php

if (isset($_POST['multiply'])) {

     $a = $_POST['packs'];
     $b = $_POST['price'];
     $c = $_POST['months'];

     $number = $a * 4.3333333 * $c * $b;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/205216-calculator-form-help/
Share on other sites

Your echo is above your setting of the variable.

 

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">

<input type="text" name="packs" value="" />How many packs a week do you smoke?<br>
<input type="text" name="price" value="" />How much do you pay per pack?<br>
<input type="text" name="months" value="" />Over how many months?<br>
<?php
if (isset($_POST['multiply'])) {

     $a = $_POST['packs'];
     $b = $_POST['price'];
     $c = $_POST['months'];

     $number =  $a * 4.3333333 * $c * $b;
}
echo "$number";
?>
<input type="submit" name="multiply" value="Calculate" />
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.