Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/275980-php-calculation-form-fields-help/
Share on other sites

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