473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Move Javascript Computation to PHP mail script

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
4 2018
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
16,027 Expert Mod 8TB
Where or when do you set the hidden field?

Do you get any errors?
Nov 26 '07 #3
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
16,027 Expert Mod 8TB
You have errors: document.getElementById("tax") has no properties. This is probably preventing the hidden field from being set.
Nov 27 '07 #5

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

Similar topics

4
by: Otto Haldi | last post by:
Hello to all I have a form in a php script with some inputs. With this form I like te send a javascript variable to the next php script. Every Input are transmittet to the next value but the...
1
by: Ian Davies | last post by:
I am trying to move some text by clicking a button. When clicked the button runs the java script and this will then take the value in the text field and add 10 to it to create a new value, this is...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.