Jump to content

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.