tags:

views:

49

answers:

4

Hi. I am trying to create a php estimate form for my business. I have 4 input text boxes, being description, qty, unit price and total price. Is there anyway I can show in the total price box the sum of the qty and unit prices? Thanks for your help

+1  A: 

You can use a JavaScript statement to do this as the user types.

 <input name="qty" id ="qty" type="text" onblur="sum()"/>

 <input name="price" id ="price" type="text" onblur="sum()"/>

 <input name="total" id ="total" type="text"/>


  <script type="text/javascript">
  function sum(){
      //grab the values
      qty = document.getElementById('qty').value;
      price = document.getElementById('price').value;

      document.getElementById('total').value = parseFloat(qty) * parseFloat(price);
  }
  </script>

Of course you have to validate the values entered by the user.

Vincent Ramdhanie
A: 

Do you mean while the user is filling out the form? In that case you'll need to do some javascript that handles the events on those two fields. If you want to display the product of the two numbers in some summary page after the user has submitted the form, then it should look something like this:

echo $_POST['quantity'] * $_POST['unit_price'];
Don Kirkby
A: 

No need to use Php for this as the calculation can be done in the client side using javascripts.

randika
That answer is as useful as saying "No need to use JavaScript for this as the calculation can be done in the server side using PHP."
ceejayoz
A: 

if you want to use only php, this is a good walkthrough, but as Vincent Ramdhanie mentioned, its crucial to validate the input before sending the variables to be calculated. probably best to use a regex in that validation

http://www.peachpit.com/articles/article.aspx?p=1315026

CheeseConQueso