/    Sign up×
Community /Pin to ProfileBookmark

How to get totals from an order form?

Okay, so I’ve got to get a pizza order form working for my schooling (validation anyways, not formulas/functions). I’ve set up the basic form, go the buttons and everything working but I’m not sure how to proceed now. What I want to happen, is every time the user selects something different on the form, it automatically updates the subtotals, gst, pst, and total at the bottom of the form. I believe I know how to get it to do one at a time (They select a pizza size and it returns the pizza value to the subtotal, and so on), but ideally, it should continue to add together. So, if I select a size, say a Small Pizza, 4.00 appears in the subtotal box, 0.28 appears in the GST and PST boxes, and 4.58 appears in the total. Then, when I click to add a topping (at 0.50), it automatically updates the 4 amounts.

Here’s my code:

[code=php]//HTML
<form method=”post” name=”pizzaOrderForm” action=”http://programs.siast.sk.ca/newmedia/onlinescripts/pizzaorder_olympic.php” target=”_blank” onsubmit=”return validateForm();”>
<h3>Please enter your contact information:</h3>
<p><label for=”FirstName_tf”>First Name: </label><input name=”FirstName_tf” id=”FirstName_tf” type=”text”></p>
<p><label for=”LastName_tf”>Last Name: </label><input name=”LastName_tf” id=”LastName_tf” type=”text”></p>
<p><label for=”Address_tf”>Address: </label><input name=”Address_tf” id=”Address_tf” type=”text”></p>
<p><label for=”Phone_tf”>Phone: </label><input name=”Phone_tf” id=”Phone_tf” type=”text”></p>
<p><label for=”Email_tf”>Email: </label><input name=”Email_tf” id=”Email_tf” type=”text”></p>

<h3>What size of pizza would you like to order?</h3>
<p><label onchange=”pizzaSize(this)”><input type=”radio” name=”Size_rg” value=”10″ id=”10″ />Small 10 ($4.00)</label></p>
<p><label><input type=”radio” name=”Size_rg” value=”12″ id=”12″ />Medium 12 ($5.00)</label></p>
<p><label><input type=”radio” name=”Size_rg” value=”15″ id=”15″ />Large 15 ($7.00)</label></p>

<h3>What toppings would you like?</h3>
<div id=”checkboxes”>
<p><input name=”Anchovies_cb” type=”checkbox” value=”yes” /><label for=”Anchovies_cb”>Anchovies</label></p>
<p><input name=”DoubleCheese_cb” type=”checkbox” value=”yes” /><label for=”DoubleCheese_cb”>Double Cheese</label></p>
<p><input name=”Pepperoni_cb” type=”checkbox” value=”yes” /><label for=”Pepperoni_cb”>Pepperoni</label></p>
<p><input name=”Mushroom_cb” type=”checkbox” value=”yes” /><label for=”Mushroom_cb”>Mushrooms</label></p>
</div>

<h3>Please select a payment method from the selection box.</h3>
<select id=”Payment_menu” size=”4″>
<option value=”select”>- Select Payment Method -</option>
<option value=”cash”>Cash</option>
<option value=”debit”>Debit</option>
<option value=”credit”>Credit</option>
</select>

<h3>Your order cost information:</h3>
<p><label for=”SubTotal_tb”>Subtotal: </label>
<input type=”text” name=”SubTotal_tb” id=”SubTotal_tb” value=”” readOnly=true />
<p><label for=”PST_tb”>PST: </label>
<input type=”text” name=”PST_tb” id=”PST_tb” value=”” readOnly=true />
<p><label for=”GST_tb”>GST: </label>
<input type=”text” name=”GST_tb” id=”GST_tb” value=”” readOnly=true />
<p><label for=”Total_tb”>TOTAL: </label>
<input type=”text” name=”Total_tb” id=”Total_tb” value=”” readOnly=true />

<p><input type=”submit” name=”Submit_but”></p>
</form>

//Javascript

function pizzaSize(data) {
document.getElementById (“10”).value = data.value;
}

//I only have this little bit for a function, but I figured right away it wasn’t really going to get me my desired result. I haven’t started building any other functions yet as I’m not sure how to proceed. I do have an extensive validation function set up, but I don’t think it’s necessary to paste in at this point.[/code]

Even hints or anything really that can push me in the right direction would be a lot of help! Thanks!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERApr 24.2014 — Consider this...

ID values must be unique. OK they are .

But they also must begin with a letter or a limited set of characters (like $ or _).

So your assignment of id="10" and id="12" may work on your current browser,

but it will not work on all of them and it probably will not validate as is.
Copy linkTweet thisAlerts:
@mrjaneauthorApr 24.2014 — That's fine and all, but I'll get to the nitpicky stuff a little later. Right now, my main concern is that I don't know how to build a function that will adjust as the user changes the form. I can figure out how to get each value into a function (small pizza, 2 toppings = add together and output subtotal, gst, pst, total), not too hard. What I'm having trouble trying to figure out, is how to build a function that sees the small pizza for $4 and outputs all the info, then the user decides to switch to a medium for $5, and the form adjusts automatically to new outputs. How do you make a function understand that it needs to minus the $4 and then add $5 for new totals?
Copy linkTweet thisAlerts:
@JMRKERApr 25.2014 — Maybe nitpicky to you now, but later you will forget it and wonder why things don't work right as designed.

Note, the radio buttons do not require an id value as it is mutually exclusive with the group name

There are better ways to collect the checkbox information, but I'll let you nitpick that out later.

Note that you did not include values for the additions, so I made some up to show proof of concept.

Also note that I have no idea what the PST: or GST: stands for nor how they are calculated.

I'll let you add that little bit of my nitpick to your code later.

<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8" /&gt;

&lt;title&gt; HTML5 page &lt;/title&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;!-- removed for testing purposes
&lt;form method="post" name="pizzaOrderForm"
action="http://programs.siast.sk.ca/newmedia/onlinescripts/pizzaorder_olympic.php" target="_blank" onsubmit="return validateForm();"&gt;
// --&gt;
&lt;form name="pizzaOrderForm" method="post" action="javascript:alert('success')" target="_blank" onsubmit="return false"&gt;

<i> </i>&lt;h3&gt;Please enter your contact information:&lt;/h3&gt;
<i> </i> &lt;p&gt;&lt;label for="FirstName_tf"&gt;First Name: &lt;/label&gt;&lt;input name="FirstName_tf" id="FirstName_tf" type="text"&gt;&lt;/p&gt;
<i> </i> &lt;p&gt;&lt;label for="LastName_tf"&gt;Last Name: &lt;/label&gt;&lt;input name="LastName_tf" id="LastName_tf" type="text"&gt;&lt;/p&gt;
<i> </i> &lt;p&gt;&lt;label for="Address_tf"&gt;Address: &lt;/label&gt;&lt;input name="Address_tf" id="Address_tf" type="text"&gt;&lt;/p&gt;
<i> </i> &lt;p&gt;&lt;label for="Phone_tf"&gt;Phone: &lt;/label&gt;&lt;input name="Phone_tf" id="Phone_tf" type="text"&gt;&lt;/p&gt;
<i> </i> &lt;p&gt;&lt;label for="Email_tf"&gt;Email: &lt;/label&gt;&lt;input name="Email_tf" id="Email_tf" type="text"&gt;&lt;/p&gt;

<i> </i>&lt;h3&gt;What size of pizza would you like to order?&lt;/h3&gt;
<i> </i> &lt;p&gt;&lt;label&gt;&lt;input type="radio" name="Size_rg" value="4" onchange="calc()" /&gt;Small 10 ($4.00)&lt;/label&gt;&lt;/p&gt;
<i> </i> &lt;p&gt;&lt;label&gt;&lt;input type="radio" name="Size_rg" value="5" onchange="calc()" /&gt;Medium 12 ($5.00)&lt;/label&gt;&lt;/p&gt;
<i> </i> &lt;p&gt;&lt;label&gt;&lt;input type="radio" name="Size_rg" value="6" onchange="calc()" /&gt;Large 15 ($7.00)&lt;/label&gt;&lt;/p&gt;

<i> </i>&lt;h3&gt;What toppings would you like?&lt;/h3&gt;
<i> </i> &lt;div id="checkboxes"&gt;
<i> </i> &lt;p&gt;&lt;input id="Anchovies_cb" type="checkbox" value="1.75" onchange="calc()" /&gt;&lt;label for="Anchovies_cb"&gt;Anchovies: $1.75&lt;/label&gt;&lt;/p&gt;
<i> </i> &lt;p&gt;&lt;input id="DoubleCheese_cb" type="checkbox" value="1.5" onchange="calc()" /&gt;&lt;label for="DoubleCheese_cb"&gt;Double Cheese: $1.50&lt;/label&gt;&lt;/p&gt;
<i> </i> &lt;p&gt;&lt;input id="Pepperoni_cb" type="checkbox" value="1.25" onchange="calc()" /&gt;&lt;label for="Pepperoni_cb"&gt;Pepperoni: $1.25&lt;/label&gt;&lt;/p&gt;
<i> </i> &lt;p&gt;&lt;input id="Mushroom_cb" type="checkbox" value="1" onchange="calc()" /&gt;&lt;label for="Mushroom_cb"&gt;Mushrooms: $1.00&lt;/label&gt;&lt;/p&gt;
<i> </i> &lt;/div&gt;

<i> </i>&lt;h3&gt;Please select a payment method from the selection box.&lt;/h3&gt;
<i> </i> &lt;select id="Payment_menu" size="4"&gt;
<i> </i> &lt;option value="select"&gt;- Select Payment Method -&lt;/option&gt;
<i> </i> &lt;option value="cash"&gt;Cash&lt;/option&gt;
<i> </i> &lt;option value="debit"&gt;Debit&lt;/option&gt;
<i> </i> &lt;option value="credit"&gt;Credit&lt;/option&gt;
<i> </i> &lt;/select&gt;

<i> </i>&lt;h3&gt;Your order cost information:&lt;/h3&gt;
<i> </i>&lt;p&gt;&lt;label for="SubTotal_tb"&gt;Subtotal: &lt;/label&gt;
<i> </i> &lt;input type="text" name="SubTotal_tb" id="SubTotal_tb" value="" readOnly=true /&gt;
<i> </i>&lt;p&gt;&lt;label for="PST_tb"&gt;PST: &lt;/label&gt;
<i> </i> &lt;input type="text" name="PST_tb" id="PST_tb" value="" readOnly=true /&gt;
<i> </i>&lt;p&gt;&lt;label for="GST_tb"&gt;GST: &lt;/label&gt;
<i> </i> &lt;input type="text" name="GST_tb" id="GST_tb" value="" readOnly=true /&gt;
<i> </i>&lt;p&gt;&lt;label for="Total_tb"&gt;TOTAL: &lt;/label&gt;
<i> </i> &lt;input type="text" name="Total_tb" id="Total_tb" value="" readOnly=true /&gt;

&lt;p&gt;&lt;input type="submit" name="Submit_but"&gt;&lt;/p&gt;
&lt;/form&gt;

&lt;script type="text/javascript"&gt;
function calc() {
var sum = 0;
sum += parseFloat(getRBtnName('Size_rg'));
var idList = ['Anchovies_cb','DoubleCheese_cb','Pepperoni_cb','Mushroom_cb'];
for (var i=0; i&lt;idList.length; i++) {
if (document.getElementById(idList[i]).checked) { sum += parseFloat(document.getElementById(idList[i]).value); }
}
document.getElementById ("SubTotal_tb").value = sum.toFixed(2); <br/>
// add to sum the values associated with 'PST_tb' and 'GST_tb' : NOT GIVEN in original post request <br/>
document.getElementById ("Total_tb").value = sum.toFixed(2); <br/>
}
function getRBtnName(GrpName) {
var sel = document.getElementsByName(GrpName);
var fnd = -1;
var str = '';
for (var i=0; i&lt;sel.length; i++) {
if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
}
// return fnd; // return option index of selection
// comment out next line if option index used in line above <br/>
return str;
}

&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@13thwarriorApr 25.2014 — Sorry to drop in, but quick question for mrjane. . . You had a question about arrays here http://www.webdeveloper.com/forum/showthread.php?293353-RESOLVED-How-to-change-quot-Select-quot-options-with-radio-buttons . I&#8217;m doing something similar with clothing and have everything lined up, but i can get the arrays right. How did you set yours up.

Thanks. . .
Copy linkTweet thisAlerts:
@Error404Apr 25.2014 — If the forms are on different pages and you only have to use/want/know JavaScript, then you have a few options. First, use hidden fields to store the amounts. Not the best but still an option. Second, cookies, for which there are plenty of resources as you'll want to create functions for them. This link is a good starting point. Third, local storage, although it's not really meant for storing values of just a few things, it's still an option to consider. Fourth, use AJAX to read the values and append them to a common variable, although this depends a bit on how your forms and site is set up.

Depending which door you choose, you're going to have to reset the values after the total is calculated.

If you can use/know/want to use PHP (or any server-side language for that matter) then it makes this task so much easier.
×

Success!

Help @mrjane spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...