Jump to content


Photo

PHP Calculation -- form fields help

php forms calculator

  • Please log in to reply
3 replies to this topic

#1 kjetterman

kjetterman

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 21 March 2013 - 09:04 AM

I need to make part of my form auto-populate calculated values based on user input.

 

Here is what I have so far:

 

The HTML

 

<div class="PaymentDetails">

<fieldset>

                  <legend>Payment Details</legend>



                 Ad Charge $



                  <input type="number" name="AdPrice" id="AdPrice" class="medium" value="" /><br />

                  

                  Color Charge +

                  

                  <input type="number" name="ColorCharge" id="ColorCharge" class="medium" value="" /><br />

                  

                  Web Ad +

                  <input type="number" name="WebAd" id="WebAd" class="medium" value="" /><br />

                  

                  Subtotal =



                  <input type="number" name="AdSubtotal" id="AdSubtotal" class="medium" value=<? $SubTotal ?> /><br />

                  

               Down Payment -



               <input type="number" name="DownPayment" id="DownPayment" class="medium" value="" /><br />

               

               Total =

               <input type="number" name="TotalPrice" id="TotalPrice" class="medium" value=<? $TotalPrice ?> /><br /><br />



                



               # Consec. Payments \



                 <input type="number" name="Payments" id="Payments" class="medium" value="" /><br />



                Amt Each Payment $



                <input type="number" name="Payment" id="Payment" class="medium" value=<? $Payment ?> /><br /><br />

                

                <input type="checkbox" name="ProratedCheck" id="ProratedCheck" /><label>Prorated/Length</label><input type="text" name="ProratedLength" id="ProratedLength" />

</fieldset>           

</div>

 

The PHP

 

<?
 
    $AdPrice = $_POST['AdPrice'];
    $ColorCharge = $_POST['ColorCharge'];
    $WebAd = $_POST['WebAd'] + $Number;
    
    $SubTotal = $AdPrice + $ColorCharge + $WebAd;
    
    $DownPayment = $_POST['DownPayment'];
    
    $TotalPrice = $SubTotal - $DownPayment;
    
    $Payments = $_POST['Payments'];
    
    $Payment = $TotalPrice / $Payments;
    
 
?>

 

I know that i'm probably missing a fairly big concept here... for instance:  How does the code know that a number has been entered and that a number needs to be output?

 

Warning: Division by zero  <--- i'm getting this error message also...

 

Thank you for any help or guidance you can give!


Kellie
*php freak in training!*

#2 DavidAM

DavidAM

    Advanced Member

  • Gurus
  • 1,956 posts
  • LocationSpring, TX USA

Posted 22 March 2013 - 04:29 PM

You have to check to see if the form was POSTed
<?php
if (isset($_POST)) {
  // Process user supplied data
}
To populate the fields, you have to echo them:
<input type="number" name="AdSubtotal" id="AdSubtotal" class="medium" value=<?php echo $SubTotal; ?> /><br />
Stop using short tags ("<?") while you are still learning. By default, they are turned off, which can prevent your script from working.
-- I haven't lost my mind, it's backed up on tape ... somewhere!

#3 kjetterman

kjetterman

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 26 March 2013 - 09:11 AM

Does it make a difference if I want the totals to auto-populate upon user-input without hitting a "calculate" or "submit" button?


Kellie
*php freak in training!*

#4 DaveyK

DaveyK

    Advanced Member

  • Members
  • PipPipPip
  • 288 posts
  • LocationThe Netherlands

Posted 26 March 2013 - 09:14 AM

That would be JavaScript.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com