| | |
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
![]() |
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
you can either use AJAX or have javascript refresh the page with the dollar amount in the url.
Thanks,
t.
here is the ajax code you need:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script type="text/javascript"> function Send() { function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } xmlHttp=GetXmlHttpObject(); xmlHttp.onreadystatechange=changeofState; //change the page below to you php page name var url="something.php"; //change 'variable' to the name of the javascript variable that contains the dollar amount url = url+"?amt="+variable; url = url+"&sid"+Math.random(); xmlHttp.open("GET",url,true); xmlHttp.send(null); function changeofState() { if(xmlHttp.readyState==4) { document.write('ok'); } } } </script>
Last edited by kkeith29; Nov 23rd, 2007 at 9:08 pm.
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Solved Threads: 0
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
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
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
this should do it. it worked for me:
HTML
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<input type="hidden" id="subtotal" name="subtotal" value="">
JAVASCRIPT
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
// you need to put the name of your form in place of 'formname' below document.formname.subtotal.value = '$ '+runningTotal.toFixed(2);
Last edited by kkeith29; Nov 24th, 2007 at 9:37 pm.
![]() |
Similar Threads
- Pass Javascript variable to PHP code (PHP)
- Pass a javascript variable to PHP in the same function (JavaScript / DHTML / AJAX)
- javascript variable in jsp session (JSP)
- Passing a Javascript variable to a PHP using <script> call (JavaScript / DHTML / AJAX)
- How to store javascript variable in jsp session (JSP)
- How Can I Pass A PHP Variable From One .php page to another (PHP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: People like Yaldex slow down the progress of web development
- Next Thread: javascript null..is it a string?
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate array automatically beta box browser bug calendar captchaformproblem cart close codes column css cursor date debugger decimal dependent design disablefirebug dom download dropdown element embed engine enter error events ext file firefox focus form frameworks getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 iframe index java javascript javascripthelp2020 jawascriptruntimeerror jquery jsp libcurl listbox maps masterpage media menu microsoft mimic mp4 onmouseoutdivproblem onmouseover paypal pdf php player position post problem programming progressbar prototype redirect regex safari scale scriptlets scroll search security select software sql text textarea unicode w3c website window windowofwords windowsxp






