•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 455,973 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,819 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 3741 | Replies: 11
![]() |
| |
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Rep Power: 0
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: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation:
Rep Power: 3
Solved Threads: 71
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation:
Rep Power: 3
Solved Threads: 71
•
•
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation:
Rep Power: 3
Solved Threads: 71
here is the ajax code you need:
<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:
Rep Power: 0
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
•
•
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation:
Rep Power: 3
Solved Threads: 71
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
<input type="hidden" id="subtotal" name="subtotal" value="">
JAVASCRIPT
// 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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- 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?


Hybrid Mode