Jump to content


PHP Calculation -- form fields help

php forms calculator

  • Please log in to reply
3 replies to this topic

#1 kjetterman


    Advanced Member

  • Members
  • PipPipPip
  • 41 posts

Posted 21 March 2013 - 02:04 PM

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


Here is what I have so far:




<div class="PaymentDetails">


                  <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" />






    $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!

*php freak in training!*

#2 DavidAM


    Advanced Member

  • Moderators
  • 1,977 posts
  • LocationSpring, TX USA

Posted 22 March 2013 - 09:29 PM

You have to check to see if the form was POSTed
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


    Advanced Member

  • Members
  • PipPipPip
  • 41 posts

Posted 26 March 2013 - 02:11 PM

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

*php freak in training!*

#4 DaveyK


    Advanced Member

  • Members
  • PipPipPip
  • 288 posts
  • LocationThe Netherlands

Posted 26 March 2013 - 02:14 PM

That would be JavaScript.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users