By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,644 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

Move Javascript Computation to PHP mail script

P: 4
Hi,

I have a Javascript total calculation function within a php form that uses onBlur to show the client a running total of the dollar amount of items added: http://www.microtribe.com/dev4Tony/order881x2.php

And I have a linked external PHP file/script to email the results of the form to me. All works fine but I can't figure out how to grab the total dollar amount ("sub_total") created by the Javascript with this line:
document.getElementById('sub_total').innerHTML = '$ ' + runningTotal.toFixed(2);

Is there a way to have the Javascript send this variable to my php script?

Here is the full Javascript Function:

Expand|Select|Wrap|Line Numbers
  1. /*Addition Function
  2. */
  3. var elements = new Array();
  4.  
  5. function calculatePrice(price,me,id) {
  6. var newValue = 0;
  7. if (me.value == 0) {
  8. document.getElementById('total' + id).innerHTML = '';
  9. } else if (me.value > 0) {
  10. newValue = price * me.value;
  11. document.getElementById('total' + id).innerHTML = newValue.toFixed(2);
  12. }
  13.  
  14. elements['total' + id] = newValue.toFixed(2);
  15.  
  16. calculateTotal();
  17.  
  18. }
  19.  
  20. function calculateTotal() {
  21. var runningTotal = 0;
  22. for (var strCurrentKey in elements) {
  23. runningTotal += parseFloat(elements[strCurrentKey]);
  24. }
  25.  
  26. var sub = runningTotal.toFixed(2);
  27. var tax = sub * 0.00;
  28. var total = sub * 1.08375;
  29.  
  30. document.getElementById('sub_total').innerHTML = '$ ' + runningTotal.toFixed(2);
  31. document.getElementById('tax').innerHTML = '$ ' + tax.toFixed(2);
  32. document.getElementById('grand_total').innerHTML = '$ ' + total.toFixed(2);
  33.  
  34. document.getElementById('field_subtotal').value = runningTotal.toFixed(2);
  35. document.getElementById('field_tax').value = tax.toFixed(2);
  36. document.getElementById('field_total').value = total.toFixed(2);
  37.  
  38. }
  39.  
  40.  
  41. /*Parse number to currency format:
  42. By Website Abstraction (www.wsabstract.com)
  43. and Java-scripts.net (www.java-scripts.net)
  44. */
  45.  
  46. //Remove the $ sign if you wish the parse number to NOT include it
  47. var wd;
  48. function parseelement(tempnum){
  49. wd="w";
  50. for (i=0;i<tempnum.length;i++){
  51. if (tempnum.charAt(i)=="."){
  52. wd="d";
  53. break;
  54. }
  55. }
  56. if (wd=="w") {
  57. return tempnum+".00";
  58. } else {
  59. if (tempnum.charAt(tempnum.length-2)==".") {
  60. return tempnum+"0";
  61. } else {
  62. tempnum=Math.round(tempnum*100)/100;
  63. return tempnum;
  64. }
  65. }
  66. }
  67.  
Thanks!
Tony
Nov 23 '07 #1
Share this Question
Share on Google+
4 Replies


P: 4
So it won't work to move the Javascript total into a hidden field - as below (which I tried):

In js script:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('subtotal').value = '$ ' + runningTotal.toFixed(2);
On HTML form:
[HTML]<input type="hidden" id="subtotal" name="subtotal" value="">
[/HTML]In php script:
[PHP]$sub_total = $_POST['subtotal'];[/PHP]

I am guessing it isn't possible, although I don't understand why I can get that total into a div tag, and not into a hidden field that I can then $_POST to my php form.

This part works:

js script:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('sub_total').innerHTML = '$ ' + runningTotal.toFixed(2);
  2.  
HTML form:
[HTML]<div class="price" id="sub_total" name="sub_total"> $0.00</div>[/HTML]

And using onBlur to call the js function, the total is updated everytime someone tabs out of a qty field.

So why can that total make it to the div tag and not the hidden field? If I could just get it to the hidden field, I could pass it to the php script...right?

Also - I'm confused by the naming of the js code getElementById. It seems like this code is "putting" my subtotal (the runningTotal.toFixed(2) piece?) into the div tag - correct? So I don't understand why it is call "getElement". I'm assuming that I don't really understand what's happening. Can someone explain?

Thanks for your help.

P.S. At this point I've spent a million hours on this project, just trying to figure out this last piece, so if someone has a better suggestion for how to have implemented this, I'd appreciate learning to do it the "right" way...

I have an order form that needs to calculate a running total of items ordered, and show the totals as they go (all on one page) and then send all the info by email once the order is complete. (no payment function needed).
Nov 24 '07 #2

acoder
Expert Mod 15k+
P: 16,027
Where or when do you set the hidden field?

Do you get any errors?
Nov 26 '07 #3

P: 4
no errors. when the form is submitted i get all of the values EXCEPT the total.

the .js function calculates the total and it appears on the users web page in the div tag quoted above. it will not put that same value in a hidden field though, which is then passed to my php script that mails me the value.

Where or when do you set the hidden field?

Do you get any errors?
Nov 27 '07 #4

acoder
Expert Mod 15k+
P: 16,027
You have errors: document.getElementById("tax") has no properties. This is probably preventing the hidden field from being set.
Nov 27 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.