954,141 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Move Javascript Variable to PHP

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
Thanks!
Tony

microtribe
Newbie Poster
6 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

you can either use AJAX or have javascript refresh the page with the dollar amount in the url.

kkeith29
Nearly a Posting Virtuoso
1,353 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 
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.

microtribe
Newbie Poster
6 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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.

kkeith29
Nearly a Posting Virtuoso
1,353 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

Thanks.

In js script:
document.getElementById('subtotal').value = '$ ' + runningTotal.toFixed(2);
On HTML form:

In php script:
$sub_total = $_POST['subtotal'];

Didn't work...

microtribe
Newbie Poster
6 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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>
kkeith29
Nearly a Posting Virtuoso
1,353 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

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:
$0.00

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

microtribe
Newbie Poster
6 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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

<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);
kkeith29
Nearly a Posting Virtuoso
1,353 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

Unbelievably, this didn't work.

I have -
external.js:
document.order_guide.subtotal.value = '$ '+runningTotal.toFixed(2);
php/html form:

and

???

microtribe
Newbie Poster
6 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

post your html. ill look at it and find the problem

kkeith29
Nearly a Posting Virtuoso
1,353 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 


FFT%20name.gif

Parkway Catering Order Guide nav backgroundhome Cafe menu catering online ordering nutritional information contact us weekly specials In order to submit your order, all fields below (except End Time and Billing Code) must be filled in Name :




Today's Date :



">


Company :





Event Date :

(enter mm/dd/yyyy)





Phone :







Email
:




Start
Time :





Building & Room No:





End Time :





No. of Guests :





Billing Code :




Please
refer to the CATERING
MENU
for a complete description of all items

BUSINESS BREAKFAST




QTY




Continental Breakfast



Price
Total


Classic
Continental ($2.75 pp)
$2.75



Bagel
Break ($1.75 pp)
$1.75



FFT
Continental ($5.00 pp)
$5.00
 


Healthy
Select Breakfast ($6.50 pp)
$6.50
 

QTY




Breakfast A la Carte



Price
Total


Seasonal
Fruit Platter
SMALL (3-5 people) 
 $17.50
 


Seasonal
Fruit Platter
MEDIUM (6-10 people)
$35.00
 


Seasonal
Fruit Platter
LARGE (11-15 people)
$52.50
 


Seasonal
Whole Fruit Basket
SMALL (3-8 people)
$9.50
 


Seasonal
Whole Fruit Basket

LARGE (12-15 people) 
$18.00
 

Individual
Low-Fat Yogurt Parfaits ($2.25 pp)
$2.25
 


Sliced
Smoked Salmon and Lox
SMALL (3-5 people)
$41.25
 


Sliced
Smoked Salmon and Lox
MEDIUM (6-10 people)
$82.50
 


Sliced
Smoked Salmon and Lox

LARGE (11-15 people)
$123.75
 

QTY




The Bake Shop



Price
Total


Breakfast
Pastry Assortment
SMALL (3-5 people) 
$15.00
 


Breakfast
Pastry Assortment
MEDIUM (6-10 people)
$27.50
 


Breakfast
Pastry Assortment
LARGE (11-15 people)
$42.50
 


Fresh
Baked Muffins

1/2 Dozen 
$9.00
 


Fresh
Baked Muffins

Dozen 
$18.00
 


Bagels
with Cream Cheese

1/2 Dozen 
$10.50
 


Bagels
with Cream Cheese

Dozen
$21.00
 


Donuts

1/2 Dozen
$10.50
 


Donuts

Dozen
$21.00
 

BEVERAGES



QTY



Sodas
and Waters
Based on one can per person



Price



Total



 
Assorted
Canned Sodas ($1.25 per person)
 
 


Coke
$1.25
 


Sprite
$1.25
 


Diet
Coke
$1.25
 


Diet
Sprite
$1.25
 
 
Assorted
Bottled Sodas ($1.50 per person)
 
 


Coke
$1.50
 


Sprite
$1.50
 


Diet
Coke
$1.50
 


Diet
Sprite
$1.50
 


Bottled
Water

Aquafina ($1.50 per person)
$1.50
 


Bottled
Sparkling Water

La Croix ($2.00 per person)
$2.00
 
QTY



Bottled
Juice
Based on one bottle per person



Price



Total





Bottled
Orange Juice ($2.00 per person)
$2.00
 


Bottled
Cranberry Juice ($2.00 per person)
$2.00
 


Bottled
Ruby Red Grapefruit Juice

($2.00 per person)
$2.00
 


Bottled
Apple Juice ($2.00 per person)
$2.00
 
QTY



Beverage
Carafes
Serves 5-7 people



Price



Total





Carafe
Juice Orange
$6.25
 


Carafe
Juice Ruby Red Grapefruit
$6.25
 


Carafe
Juice Apple
$6.25
 
QTY



Coffee
Freshly Brewed Premium Coffee and Decaffeinated
Coffee



Price



Total





Freshly
Brewed Premium Coffee Regular

Small Pot (6-11 people)
$16.00
 


Freshly
Brewed Premium Coffee Regular

Large Pot (12-24 people)
$32.00
 


Freshly
Brewed Premium Coffee DECAF

Small Pot (6-11 people)
$16.00
 


Freshly
Brewed Premium Coffee DECAF

Large Pot (12-24 people)
$32.00
 
QTY



Hot
Tea Service



Price



Total





Small
Pot (6-11 people)
$16.00
 

WORKING LUNCH BUFFET




QTY




Sandwich and Gourmet Wrap Tray



Price
Total


SMALL
TRAY (3-5 people) 
$48.75
 


MEDIUM
TRAY (6-10 people)
$97.50
 


LARGE
TRAY (11-15 people)
$146.25
 
 
*Choice
of One:
Fresh Fruit SaladMixed GreensPasta Salad
 
 

QTY




Petite Sandwich Assortment



Price
Total


SMALL
ASSORTMENT (3-5 people) 
$42.50
 


MEDIUM
ASSORTMENT (6-10 people)
$85.00
 


LARGE
ASSORTMENT (11-15 people)
$127.50
 
 
*Choice
of One:
Fresh Fruit SaladMixed GreensPasta Salad
 
 

QTY




The FFT Premium Deli Tray



Price
Total


SMALL
TRAY (3-5 people) 
$50.00
 


MEDIUM
TRAY (6-10 people)
$100.00
 


LARGE
TRAY (11-15 people)
$150.00
 
 
*Choice
of One:
Fresh Fruit SaladMixed GreensPasta Salad
 
 

QTY




Antipasto Deli Tray



Price
Total


SMALL
TRAY (3-5 people) 
$57.50
 


MEDIUM
TRAY (6-10 people)
$115.00
 


LARGE
TRAY (11-15 people)
$172.50
 
 
*Choice
of One:
Fresh Fruit SaladMixed GreensPasta Salad
 
 

INDIVIDUAL BOXED LUNCHES




QTY




Entrée Salads



Price
Total


Chicken
Caesar Salad
$9.75
 


Signature
Chopped Salad
$9.75
 


Chef's
Salad
$9.75
 


Tuna
Niçoise Salad
$9.75
 


Salad
Trio
$9.75
 


Apple
Chicken Salad
$9.75
 


Mandarin
Salad
$9.75
 


Mediterranean
Salad
$9.75
 


Grilled
Buffalo Chicken Salad
$9.75
 

QTY




Premium Sandwiches



Price
Total


Ham
& Sliced Swiss
$9.50
 


Low-Fat
Tuna Salad
$9.50
 


Smoked
Turkey Breast
$9.50
 


Slow
Roasted Beef
$9.50
 


Grilled
Chicken Salad
$9.50
 


Italiano
$9.50
 

QTY




Gourmet Wraps



Price
Total


Grilled
Chicken Caesar
$9.50
 


Mediterranean
Vegetable
$9.50
 

Mandarin
Chicken
$9.50
 

Grilled
Buffalo Chicken
$9.50
 

HOT BUFFET SELECTIONS



QTY

Minimum
of 8 Guests

$11.50 Per Person



Price



Total




Milano
$11.50
 

Mykonos
$11.50
 

Tex
Mex
$11.50
 

Italia
$11.50
 

San
Miguel
$11.50
 

Asian
Skillet
$11.50
 

Marsala
$11.50
 

MEETING BREAKS & SNACKS



QTY



Gourmet
Displays



Price



Total




Hummus
with Pita Bread

MEDIUM (6—10 people)
$27.50
 

Hummus
with Pita Bread

LARGE (11—15 people)
$41.25
 

Caramelized
Onion and Spinach Dip

MEDIUM (6—10 people)
$26.75
 

Caramelized
Onion and Spinach Dip

LARGE (11-15 people)
$40.00
 

Salsa
and Tortilla Chips

MEDIUM (6—10 people)
$23.00
 

Salsa
and Tortilla Chips

LARGE (11-15 people)
$34.50
 

Domestic
Cheese Selection

MEDIUM (6—10 people)
$47.00
 

Domestic
Cheese Selection

LARGE (11-15 people)
$70.50
 

Crisp
Garden Crudités

MEDIUM (6—10 people)
$25.00
 

Crisp
Garden Crudités

LARGE (11-15 people)
$37.50
 

Seasonal
Fruit Platter

SMALL (3—5 people)
$17.50
 

Seasonal
Fruit Platter

MEDIUM (6—10 people)
$35.00
 

Seasonal
Fruit Platter

LARGE (11-15 people)
$52.50
 
QTY



Individual
Bagged Snacks
Priced Per Person



Price



Total




Mini
Pretzels
$1.00
 

Cajun
Snack Mix
$1.75
 

Popcorn
$1.00
 

Trail
Mix
$2.75
 

Assorted
Mixed Nuts
$4.25
 

Assorted
Candy Bars
$1.50
 

Premium
Brand Bagged Chips
$1.25
 

Assorted
Granola Bars
$1.50
 

Assorted
Ice Cream Bars
$2.00
 
QTY



Dessert
Trays



Price



Total




Assorted
Home-style Cookies

MEDIUM (6—10 people)
$15.00
 

Assorted
Home-style Cookies

LARGE (11—15 people)
$22.50
 

Home-style
Bars

MEDIUM (6—10 people)
$25.00
 

Home-style
Bars

LARGE (11-15 people)
$37.50
 

Petite
Sweets

MEDIUM (6—10 people)
$25.00
 

Petite
Sweets

LARGE (11-15 people)
$50.00
 

Total*:
$0.00


*
Tax and Delivery Charges will be applied to your final bill.


Additional Comments**:






**Special orders can be subject to additional
charges

To
keep a hard copy of your order, please PRINT this page prior to
pressing the SUBMIT button below.




 

ORDERING
SPECIFICS

Food For Thought’s Conference
Catering Menus allow you to plan a satisfying meeting or group celebration
without having to leave your office. All catering orders will be delivered
with disposable plates, cups, flatware, napkins, serving utensils and
condiments.

All Express Catering requires 24 hours notice.

Delivery Charge for Drop-off Catering

Delivery fee for all orders within the building $5.00

Pick-up fee for any order requiring a Porter to clear after $5.00

Cancellation Policy

There is no charge for cancellations made by 2:00pm the day prior to
the event.

Same day cancellation is subject to a 100% charge.

We gladly accept Visa &
MasterCard.



 
 
 
 


Return
to Main Page




QTY





Sodas and waters
based on one can per person




Coming
Soon

asdflkjhlkjhsdf

lkajshdflkj

asdfl;kjhlasdf



microtribe
Newbie Poster
6 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

well, for the javascript. you don't need any of the code i gave you. that was for sending one value to php not the entire form. it could if you wanted it to without refreshing the page. just private message me the javascript. i'll put it all together and get you a working script soon.

kkeith29
Nearly a Posting Virtuoso
1,353 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You