Jump to content

Field Calculations


Wayniac

Recommended Posts

I am trying to get the quantity to multiply with the price and to equal the sub_total. The problem is getting it to instantly calculate with the field. So as soon as the user enters in the price and quantity, I want the sub_total to instantantly calculate. I was thinking of setting up a function and an onClick for the sub_total. I just can't seem to get it to work, nothing is changing so I am clearly missing a step.

 

$quantity = mysql_real_escape_string(stripslashes($_POST['quantity']));
  
$price = mysql_real_escape_string(stripslashes($_POST['price']));

$sub_total = mysql_real_escape_string(stripslashes($_POST['sub_total']));

 

I placed these quick variables in to see if the fields would even accept any values.

 

$price = 2.99;
$quantity = 100;	  
$sub_total = $quantity * $price;

 

Here are the fields...

<input name="quantity" id="quantity" size="35" maxlength="255" />
<input name="price" id="price" size="35" maxlength="255" />
<input name="sub_total" id="sub_total" size="35" maxlength="255" />

 

Any suggestions would be greatly appreciated...

Link to comment
https://forums.phpfreaks.com/topic/199073-field-calculations/
Share on other sites

<script>
function sub_total(){
qty = document.getElementById('quantity').value;
if(!qty){
    qty = 0;
}
price = document.getElementById('price').value;
if(!price){
    price = 0;
}
document.getElementById('sub_total').value = qty * price;
}
</script>

<input name="quantity" id="quantity" size="35" maxlength="255" onkeyup="sub_total();" />
<input name="price" id="price" size="35" maxlength="255" onkeyup="sub_total();" />
<input name="sub_total" id="sub_total" size="35" maxlength="255" />

 

Works like a charm. Enjoy

Link to comment
https://forums.phpfreaks.com/topic/199073-field-calculations/#findComment-1044990
Share on other sites

Whoops sorry, I forgot IE is a little fussy. :D

 

Works more than a charm now ;)

<script>
function sub_total(){
var qty = document.getElementById('quantity').value;
if(!qty){
    var qty = 0;
}
var price = document.getElementById('price').value;
if(!price){
    var price = 0;
}
document.getElementById('sub_total').value = qty * price;
}
</script>

<input name="quantity" id="quantity" size="35" maxlength="255" onkeyup="sub_total();" />
<input name="price" id="price" size="35" maxlength="255" onkeyup="sub_total();" />
<input name="sub_total" id="sub_total" size="35" maxlength="255" />

Link to comment
https://forums.phpfreaks.com/topic/199073-field-calculations/#findComment-1044997
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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