Wayniac Posted April 19, 2010 Share Posted April 19, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/199073-field-calculations/ Share on other sites More sharing options...
Domcsore Posted April 19, 2010 Share Posted April 19, 2010 PHP is server side, there for to do this you will need to use a form and use post or get method linking to a different page. Alternatively look up AJAX. I think I am right in what you mean. Quote Link to comment https://forums.phpfreaks.com/topic/199073-field-calculations/#findComment-1044905 Share on other sites More sharing options...
Wayniac Posted April 20, 2010 Author Share Posted April 20, 2010 Sorry Dom, not sure I follow on the first part. Quote Link to comment https://forums.phpfreaks.com/topic/199073-field-calculations/#findComment-1044967 Share on other sites More sharing options...
Pikachu2000 Posted April 20, 2010 Share Posted April 20, 2010 PHP is parsed on the server, not on the client machine. You need a client-side technology to do what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/199073-field-calculations/#findComment-1044982 Share on other sites More sharing options...
havenpets Posted April 20, 2010 Share Posted April 20, 2010 You need javascript for this, not PHP. Javascript can grab the value of one input and multiply it to another to equal another value of a different input. If that makes sense... Quote Link to comment https://forums.phpfreaks.com/topic/199073-field-calculations/#findComment-1044984 Share on other sites More sharing options...
havenpets Posted April 20, 2010 Share Posted April 20, 2010 <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 Quote Link to comment https://forums.phpfreaks.com/topic/199073-field-calculations/#findComment-1044990 Share on other sites More sharing options...
Wayniac Posted April 20, 2010 Author Share Posted April 20, 2010 Thank you both for your replies. havenpets, I tried that code but nothing worked, I even tried changing it to an onClick, but no go... Quote Link to comment https://forums.phpfreaks.com/topic/199073-field-calculations/#findComment-1044995 Share on other sites More sharing options...
havenpets Posted April 20, 2010 Share Posted April 20, 2010 Whoops sorry, I forgot IE is a little fussy. 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" /> Quote Link to comment https://forums.phpfreaks.com/topic/199073-field-calculations/#findComment-1044997 Share on other sites More sharing options...
Wayniac Posted April 20, 2010 Author Share Posted April 20, 2010 Thank you heaven, unfortunately I could'nt get that to work either. Should I be putting the script in the header? Everything seems like it should work but nothing at all is happening... Quote Link to comment https://forums.phpfreaks.com/topic/199073-field-calculations/#findComment-1045006 Share on other sites More sharing options...
havenpets Posted April 20, 2010 Share Posted April 20, 2010 http://havenpets.tridiantgames.com/test/jstest.php - Example It's working fine on Firefox, Internet Explorer and Google Chrome... Take a look for yourself. Quote Link to comment https://forums.phpfreaks.com/topic/199073-field-calculations/#findComment-1045248 Share on other sites More sharing options...
Wayniac Posted April 20, 2010 Author Share Posted April 20, 2010 Yes your right. You can't imagine how much I appreciate you going through the effort to do that for me. I will keep this forum updated when I figure out what my mistake is. Quote Link to comment https://forums.phpfreaks.com/topic/199073-field-calculations/#findComment-1045294 Share on other sites More sharing options...
Wayniac Posted April 22, 2010 Author Share Posted April 22, 2010 Thank you so much havenpets! I got your code working, the issue I was having was putting the Javascript within the form. I placed it outside and worked marvously. Once again, thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/199073-field-calculations/#findComment-1046439 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.