-
November 25th, 2007, 10:49 AM
#1
Pass Javascript variable to hidden field and php form
I have a Javascript total calculation function in an external .js file called from a php form using 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 an 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") that is calculated by the Javascript and returned to a div tag on my form with this line:
document.getElementById('sub_total').innerHTML = '$ ' + runningTotal.toFixed(2);
Is there a way to have the Javascript send this variable to a hidden field on my form which I can then pass to my php script to email? I am able to get the total into a div tag, but not a hidden field.
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.order_guide.subtotal.value = '$ '+runningTotal.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);
}
Here are relevant bits of the php form:
<form enctype="multipart/form-data" name="order_guide" action="sendorder.php" method="post">
<input name="subtotal" type="hidden" id="subtotal" value="">
And relevant bits of my php script:
$sub_total = $_POST['subtotal'];
$msg .= "ORDER SUBTOTAL: $sub_total\n";
Thanks!
Tony
Last edited by PeejAvery; December 2nd, 2007 at 02:23 PM.
Reason: Added code tags.
-
November 26th, 2007, 01:11 AM
#2
Re: Pass Javascript variable to hidden field and php form
Have u try this:
Code:
document.getElementById("subtotal").value = total;
Muhammad Waqas Badar
-
November 26th, 2007, 01:46 PM
#3
Re: Pass Javascript variable to hidden field and php form
Thanks. I tried that but it didn't work...Any other ideas?
-
November 27th, 2007, 01:24 AM
#4
Re: Pass Javascript variable to hidden field and php form
why there is the enctype="multipart/form-data" attribute in the form? usually, we don't specify enctype for POST method. you may try to remove that as it is intended for file upload..
Code:
<form name="order_guide" action="sendorder.php" method="post">
<input name="subtotal" type="hidden" id="subtotal" value="">
Busy
-
December 2nd, 2007, 02:30 PM
#5
Re: Pass Javascript variable to hidden field and php form
Waqas_Badar's suggestion is correct. Are you sure you are using it right? Also, you can alert the hidden input after you set it to make sure the data is set properly.
Code:
var objSubTotal = document.getElementById('subtotal');
objSubTotal.value = total;
alert(objSubTotal.value);
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
September 27th, 2012, 01:30 PM
#6
Re: Pass Javascript variable to hidden field and php form
Thanks this helped a lot on my project!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|