Move Javascript Variable to PHP

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Nov 2007
Posts: 6
Reputation: microtribe is an unknown quantity at this point 
Solved Threads: 0
microtribe microtribe is offline Offline
Newbie Poster

Move Javascript Variable to PHP

 
0
  #1
Nov 23rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Move Javascript Variable to PHP

 
0
  #2
Nov 23rd, 2007
you can either use AJAX or have javascript refresh the page with the dollar amount in the url.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: microtribe is an unknown quantity at this point 
Solved Threads: 0
microtribe microtribe is offline Offline
Newbie Poster

Re: Move Javascript Variable to PHP

 
0
  #3
Nov 23rd, 2007
Originally Posted by kkeith29 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Move Javascript Variable to PHP

 
0
  #4
Nov 23rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: microtribe is an unknown quantity at this point 
Solved Threads: 0
microtribe microtribe is offline Offline
Newbie Poster

Re: Move Javascript Variable to PHP

 
0
  #5
Nov 23rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Move Javascript Variable to PHP

 
0
  #6
Nov 23rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: microtribe is an unknown quantity at this point 
Solved Threads: 0
microtribe microtribe is offline Offline
Newbie Poster

Re: Move Javascript Variable to PHP

 
0
  #7
Nov 24th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Move Javascript Variable to PHP

 
0
  #8
Nov 24th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: microtribe is an unknown quantity at this point 
Solved Threads: 0
microtribe microtribe is offline Offline
Newbie Poster

Re: Move Javascript Variable to PHP

 
0
  #9
Nov 25th, 2007
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="">

???
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Move Javascript Variable to PHP

 
0
  #10
Nov 25th, 2007
post your html. ill look at it and find the problem
Last edited by kkeith29; Nov 25th, 2007 at 12:40 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC