tags:

views:

18

answers:

3

I am quite new to php. Can somebody please guide me what is wrong with the code.

<?php
    if(!isset($_POST['submit']) || $_POST['submit']!="calculate")
    {
       $_POST['Contrib']="";
       $_POST['Currentage']="";
       $_POST['Retireage']="";
       $Total =0;
       $AnnGain =7;
    }else{
       $AnnGain = $_POST['AnnGain'];
       $Years = $_POST['Retireage'] - $_POST['Currentage'];
       $YearCount = 0;
       $Total = $_POST['Contrib'];

       while ($YearCount < $Years)
       {
           $Total = (round($Total) *(1.0 + $AnnGain/100) +
               $_POST['Contrib']);
           $YearCount = $YearCount+1;

       }

    }
    ?>

           <b>A Retirement Saving calculator</b>


           <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
           <p> Your age now
           <input type="text" size = "5" name = "Currentage"
                  value="<?php echo $_POST['Currentage'];?>">
           <p> The age at which you want to retire
           <Input type="text" SIZE="6" name="Retireage"
                  value="<?php echo $_POST['Retireage']; ?>">
           <p> Annual Contribution
           <input type="text" size = "15" name = "Contrib"
                  value="<?php echo $_POST['Contrib'];?>">
           <p>Annual Return
           <input type = "text" size = "5" NAME = "AnnGain"
                  value="<?php echo$AnnGain; ?>">
           <BR><BR>
           <p><b>Nest Egg </b>: <?php echo $Total; ?>
           <p><Input type = "submit" Name = "submit" value = "calculate">
           </form>
A: 

In your code i see:

input type="test"

It is wrong, it should be:

input type="text"

Input type should be text if you mean a textbox.

Sarfraz
Thanks for the typo. still the code is not working as expected
A: 
   $_POST['Currentage']=="";
   $_POST['Retireage']=="";

You're checking if $_POST['Currentage'] is equal to "" instead of setting it to "". What you want is $_POST['Currentage'] = "";. You have the same problem with $_POST['Retireage'].

Brendan Long
A: 

outside of the fact that modifying $_POST variables is bad practice (just assign those values to a variable and use that in your code)

  1. $_POST['Currentage']==""; should be $_POST['Currentage'] = '';
  2. $_POST['Retireage']==""; should be $_POST['Retireage'] = '';
  3. ALWAYS escape data using something like htmlentities() before you spit it out to the web browser to protect your page from injections. This is VERY important
AndrewMurphy
Thanks for your suggestions. Still something is missing.