kjetterman Posted March 21, 2013 Share Posted March 21, 2013 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 More sharing options...
DavidAM Posted March 22, 2013 Share Posted March 22, 2013 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. Link to comment https://forums.phpfreaks.com/topic/275980-php-calculation-form-fields-help/#findComment-1420377 Share on other sites More sharing options...
kjetterman Posted March 26, 2013 Author Share Posted March 26, 2013 Does it make a difference if I want the totals to auto-populate upon user-input without hitting a "calculate" or "submit" button? Link to comment https://forums.phpfreaks.com/topic/275980-php-calculation-form-fields-help/#findComment-1421143 Share on other sites More sharing options...
DaveyK Posted March 26, 2013 Share Posted March 26, 2013 That would be JavaScript. Link to comment https://forums.phpfreaks.com/topic/275980-php-calculation-form-fields-help/#findComment-1421146 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.