lovephp Posted February 3, 2016 Share Posted February 3, 2016 anyone here good at js? im looking for a form with following fields which would auto calculate and display the total on fly. <input type="text" name="acutalprice" value="329">Actual Price: 329 <input type="text" name="discount" value="65">Discount: 65% <input type="text" name="shipping" value="50">Shipping: 50+ Total Amount: 165.15 on sumbit Regards Quote Link to comment https://forums.phpfreaks.com/topic/300724-need-a-small-help-in-auto-calculation-form-anyone/ Share on other sites More sharing options...
lovephp Posted February 4, 2016 Author Share Posted February 4, 2016 well i got what i needed but got a little issue i want to add the results inside a input text field rather than <h3 id="result"></h3>? also display the results on keyup event while i input the number in other fields? something like <input type="text" name="totalamount" value="results" readonly/> here my code <form id="myForm"> <input id="actualprice" type="number" placeholder="Actual Price"> <input id="discount" type="number" placeholder="Discount"> <input id="shipping" type="number" placeholder="Shipping"> <input type="submit" value="Submit"> </form> <h3 id="result"></h3> <script> $('#myForm').submit(function (event) { event.preventDefault(); var actualprice = Number($("#actualprice").val().trim()); var discount = Number($("#discount").val().trim()); var shipping = Number($("#shipping").val().trim()); var discountRate = (100 - discount) / 100; var result = (actualprice * discountRate) + shipping; $("#result").html("Result :" + result.toFixed(2)); }); </script> Quote Link to comment https://forums.phpfreaks.com/topic/300724-need-a-small-help-in-auto-calculation-form-anyone/#findComment-1530779 Share on other sites More sharing options...
Muddy_Funster Posted February 5, 2016 Share Posted February 5, 2016 Something like the following: <form id="myForm"> <input id="actualprice" type="number" placeholder="Actual Price"> <input id="discount" type="number" placeholder="Discount"> <input id="shipping" type="number" placeholder="Shipping"> <input id="total" type="text" name="totalamount" disabled="disabled" /> <input type="submit" value="Submit"> </form> <h3 id="result"></h3> <script> $('#myForm>input').on('keyup', function () { var actualprice = Number($("#actualprice").val().trim()); var discount = Number($("#discount").val().trim()); var shipping = Number($("#shipping").val().trim()); var discountRate = (100 - discount) / 100; var result = (actualprice * discountRate) + shipping; $("#total").val("Result :" + result.toFixed(2)); }); </script> Quote Link to comment https://forums.phpfreaks.com/topic/300724-need-a-small-help-in-auto-calculation-form-anyone/#findComment-1530793 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.