Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Move Javascript Calculation to PHP mail script
Old 11-23-2007, 11:36 AM Move Javascript Calculation to PHP mail script
Novice Talker

Posts: 5
Name: Tony
Trades: 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:

/*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;
}
}
}

Thanks!
Tony
microtribe is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-23-2007, 12:09 PM Re: Move Javascript Calculation to PHP mail script
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
No, it cannot be done the way you want it.
PHP is a server side language that is active only (mostly..) during page load.
Javascript is a browser side language that is active mostly during the loading and existing time of the page.

You can send a js variable to php via an ajax call, but this variable won't be available in the php of your current page because that this means the page is already loaded.

The only way to achieve what you want is to reload the page including the value either in the url (GET request, not recommended for obvious security reasons) or into a POST form.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-24-2007, 02:40 PM Re: Move Javascript Calculation to PHP mail script
Novice Talker

Posts: 5
Name: Tony
Trades: 0
Thanks for your reply.

So it won't work to move the Javascript total into a hidden field - as below (which I tried):

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'];

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:
document.getElementById('sub_total').innerHTML = '$ ' + runningTotal.toFixed(2);

HTML form:
<div class="price" id="sub_total" name="sub_total"> $0.00</div>

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).
microtribe is offline
Reply With Quote
View Public Profile
 
Old 11-24-2007, 04:56 PM Re: Move Javascript Calculation to PHP mail script
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
So why can that total make it to the div tag and not the hidden field?
I don't see where you tried it, but I can assure you it works.
But, to provide a value to a form field, you don't use innerHTML, but .value:
Code:
document.getElementById('hiddenField').value=1234;
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-24-2007, 07:44 PM Re: Move Javascript Calculation to PHP mail script
Novice Talker

Posts: 5
Name: Tony
Trades: 0
I did try using .value:
document.getElementById('subtotal').value = '$ ' + runningTotal.toFixed(2);

And it doesn't post to the hidden field:
<input name="subtotal" type="hidden" id="subtotal" value="">

Thanks,

Tony
microtribe is offline
Reply With Quote
View Public Profile
 
Old 11-24-2007, 07:50 PM Re: Move Javascript Calculation to PHP mail script
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Why are you happening "$ " to your value ???
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-25-2007, 11:14 AM Re: Move Javascript Calculation to PHP mail script
Novice Talker

Posts: 5
Name: Tony
Trades: 0
I'm using $ to add $ before the amount. It's not causing the problem.
microtribe is offline
Reply With Quote
View Public Profile
 
Old 11-25-2007, 03:54 PM Re: Move Javascript Calculation to PHP mail script
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Then I don't see why it don't work...
Your code is correct.

Except, maybe, if there is another HTML element with an id "subtotal", it could be that one being updated.
You could check for that, but otherwise, the problem don't seems to lies in that code.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Move Javascript Calculation to PHP mail script
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.58022 seconds with 12 queries