943,939 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Nov 23rd, 2007
0

Move Javascript Variable to PHP

Expand Post »
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:
<code>

/*Addition Function
*/
var elements = new Array();

function calculatePrice(price,me,id) {
var newValue = 0;
if (me.value == 0) {
document.getElementById('total' + id).innerHTML = '';
} else if (me.value > 0) {
newValue = price * me.value;
document.getElementById('total' + id).innerHTML = newValue.toFixed(2);
}

elements['total' + id] = newValue.toFixed(2);

calculateTotal();

}

function calculateTotal() {
var runningTotal = 0;
for (var strCurrentKey in elements) {
runningTotal += parseFloat(elements[strCurrentKey]);
}

var sub = runningTotal.toFixed(2);
var tax = sub * 0.00;
var total = sub * 1.08375;

document.getElementById('sub_total').innerHTML = '$ ' + runningTotal.toFixed(2);
document.getElementById('tax').innerHTML = '$ ' + tax.toFixed(2);
document.getElementById('grand_total').innerHTML = '$ ' + total.toFixed(2);

document.getElementById('field_subtotal').value = runningTotal.toFixed(2);
document.getElementById('field_tax').value = tax.toFixed(2);
document.getElementById('field_total').value = total.toFixed(2);

}


/*Parse number to currency format:
By Website Abstraction (www.wsabstract.com)
and Java-scripts.net (www.java-scripts.net)
*/

//Remove the $ sign if you wish the parse number to NOT include it
var wd;
function parseelement(tempnum){
wd="w";
for (i=0;i<tempnum.length;i++){
if (tempnum.charAt(i)=="."){
wd="d";
break;
}
}
if (wd=="w") {
return tempnum+".00";
} else {
if (tempnum.charAt(tempnum.length-2)==".") {
return tempnum+"0";
} else {
tempnum=Math.round(tempnum*100)/100;
return tempnum;
}
}
}
</code>
Thanks!
Tony
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
microtribe is offline Offline
6 posts
since Nov 2007
Nov 23rd, 2007
0

Re: Move Javascript Variable to PHP

you can either use AJAX or have javascript refresh the page with the dollar amount in the url.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Nov 23rd, 2007
0

Re: Move Javascript Variable to PHP

Click to Expand / Collapse  Quote originally posted by kkeith29 ...
you can either use AJAX or have javascript refresh the page with the dollar amount in the url.
Thanks for the reply, but I'm looking for some help. I'm aware that I can do it using AJAX, but I don't know how. Having Javascript refresh the page doesn't soung like it's going to help me get the variable into the email I'm sending from my php script.

Thanks,

t.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
microtribe is offline Offline
6 posts
since Nov 2007
Nov 23rd, 2007
0

Re: Move Javascript Variable to PHP

sorry i didn't go into enough detail. when you put the dollar amount in the url, php can access it through the $_GET method. i will make some code for you and post it soon.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Nov 23rd, 2007
0

Re: Move Javascript Variable to PHP

Thanks.

In js script:
document.getElementById('subtotal').value = '$ ' + runningTotal.toFixed(2);
On HTML form:
<input type="hidden" id="subtotal" name="subtotal" value="">
In php script:
$sub_total = $_POST['subtotal'];

Didn't work...
Last edited by microtribe; Nov 23rd, 2007 at 7:59 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
microtribe is offline Offline
6 posts
since Nov 2007
Nov 23rd, 2007
0

Re: Move Javascript Variable to PHP

here is the ajax code you need:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. function Send() {
  3. function GetXmlHttpObject()
  4. {
  5. var xmlHttp=null;
  6. try
  7. {
  8. // Firefox, Opera 8.0+, Safari
  9. xmlHttp=new XMLHttpRequest();
  10. }
  11. catch (e)
  12. {
  13. // Internet Explorer
  14. try
  15. {
  16. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  17. }
  18. catch (e)
  19. {
  20. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  21. }
  22. }
  23. return xmlHttp;
  24. }
  25. xmlHttp=GetXmlHttpObject();
  26. xmlHttp.onreadystatechange=changeofState;
  27.  
  28. //change the page below to you php page name
  29.  
  30. var url="something.php";
  31.  
  32. //change 'variable' to the name of the javascript variable that contains the dollar amount
  33.  
  34. url = url+"?amt="+variable;
  35. url = url+"&sid"+Math.random();
  36. xmlHttp.open("GET",url,true);
  37. xmlHttp.send(null);
  38. function changeofState()
  39. {
  40. if(xmlHttp.readyState==4)
  41. {
  42. document.write('ok');
  43. }
  44. }
  45. }
  46. </script>
Last edited by kkeith29; Nov 23rd, 2007 at 9:08 pm.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Nov 24th, 2007
0

Re: Move Javascript Variable to PHP

Thanks. That looked great, and it's over my head at this point, but it didn't work out...I don't understand so I can't ask any intelligent questions. I did replace the url and variable name, but it didn't work.

You know, I may be able to figure this out if I can understand how the Javascript gets the total amount to the div tag with this piece of code:
document.getElementById('sub_total').innerHTML = '$ ' + runningTotal.toFixed(2);

It would make sense from the code naming that it is "getting" something, not "putting" something.

But the amount shows up on the page through the div tag:
<div class="price" id="sub_total" name="sub_total"> $0.00</div>

However, I can't seem to get that same total into a hidden field. So that if I have a hidden field, and use the code referenced a couple posts above, I can get a fixed number from the hidden field to my php form, so that part works, but I can't get the Javascript total into the hidden field. If I could just do that, it would work...

Thanks a lot for your help. I appreciate the script.

Tony
Reputation Points: 10
Solved Threads: 0
Newbie Poster
microtribe is offline Offline
6 posts
since Nov 2007
Nov 24th, 2007
0

Re: Move Javascript Variable to PHP

oh ok i see what you are trying to do. I am going about it all wrong. you are sending the sub total value along with the form via a hidden field. i guess i didn't read into your problem very well.

this should do it. it worked for me:

HTML
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input type="hidden" id="subtotal" name="subtotal" value="">

JAVASCRIPT
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // you need to put the name of your form in place of 'formname' below
  2. document.formname.subtotal.value = '$ '+runningTotal.toFixed(2);
Last edited by kkeith29; Nov 24th, 2007 at 9:37 pm.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Nov 25th, 2007
0

Re: Move Javascript Variable to PHP

Unbelievably, this didn't work.

I have -
external.js:
document.order_guide.subtotal.value = '$ '+runningTotal.toFixed(2);
php/html form:
<form enctype="multipart/form-data" name="order_guide" action="sendorder.php" method="post">

and

<input name="subtotal" type="hidden" id="subtotal" value="">

???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
microtribe is offline Offline
6 posts
since Nov 2007
Nov 25th, 2007
0

Re: Move Javascript Variable to PHP

post your html. ill look at it and find the problem
Last edited by kkeith29; Nov 25th, 2007 at 12:40 pm.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: People like Yaldex slow down the progress of web development
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: javascript null..is it a string?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC