achesnpains Posted May 28, 2013 Share Posted May 28, 2013 Ok so I've watch a ton of tutorials, read a few books asked a whole bunch of questions and still can't find a solution. Here is my problem, I have this form here : http://jemtechnv.com/beta/finish_ticket.php if you play with the form you will see that entering input in the Qty, and Price boxes will populate the Ext box along with sales tax and material cost and Material boxes, and input in the rate and hours box will populate the subtotal and labor boxes now here is my problem. I need to auto populate the total hours box by from the sum of the hours column and I also need to sum up the material and labor boxes and populate that total in the grand total box, sounds easy huh? you would think but here is my code that I use to calculate my sales tax : function tax(){ var materialcost = document.getElementById( 'materialcost' ).value; var shipping = document.getElementById( 'shipping' ).value; var salestax = Math.round(((materialcost / 100) * 8.1)*100)/100; var materialtotal = (materialcost * 1) + (salestax * 1) + (shipping * 1); document.getElementById( 'materialcost' ).value = materialcost; document.getElementById( 'salestax' ).value = salestax; document.getElementById( 'shipping' ).value = shipping; document.getElementById( 'materialtotal' ).value = materialtotal; } this works fine but when I try to use the materialtotal in another function to total up the materialtotal and labortotals for the grand total it causes the browser to lock up. Can someone tell me how I can get my grand total without making the script too complicated? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/278461-need-help-with-vars/ Share on other sites More sharing options...
Irate Posted May 28, 2013 Share Posted May 28, 2013 Functions shouldn't affect global variables. You can define the variables outside of the functions and pass a reference to the variables inside of the function. Quote Link to comment https://forums.phpfreaks.com/topic/278461-need-help-with-vars/#findComment-1432672 Share on other sites More sharing options...
achesnpains Posted May 28, 2013 Author Share Posted May 28, 2013 (edited) I've tried that and it locks up the browser. Any 2nd reference to the materialtotal field immediately locks up the browser when the script loads, it's almost as if it's stuck in an indefinite loop and causes the browser to stick at that point. If I remove the 2nd reference to materialtotal of the 2nd function the browser doesn't lock up but then I don't get the desired result! Edited May 28, 2013 by achesnpains Quote Link to comment https://forums.phpfreaks.com/topic/278461-need-help-with-vars/#findComment-1432828 Share on other sites More sharing options...
Solution nogray Posted May 29, 2013 Solution Share Posted May 29, 2013 You can simply ready the input value for the total in your other function, e.g. function my_other_function(){ var materialtotal = document.getElementById( 'materialtotal' ).value } Quote Link to comment https://forums.phpfreaks.com/topic/278461-need-help-with-vars/#findComment-1432868 Share on other sites More sharing options...
Irate Posted May 29, 2013 Share Posted May 29, 2013 Also, when you're operating with values of input field, parseFloat() or parseInt() the valu and save it in a variable, that way, you don't need to iterate over the same value over and over again. Quote Link to comment https://forums.phpfreaks.com/topic/278461-need-help-with-vars/#findComment-1432914 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.