sfidirectory
Senior Member
- Mar 29, 2010
- 931
- 504
Hi everyone,
So this is my first real crack at Javascript, for this example I have tried to make an order form with Javascript that does the client-side validation of form fields. What I am wanting to do is when the user selects a "quantity" from the dropdown menu, the subtotal is automatically calculated and produced under a "subtotals" heading (for some reason, this heading is not displayed and that is most likely because of the Javascript & html hooks). This is done for each product listed in the form. Then finally I am wanting to add all the subtotals for each product to calculate the final total (ideally I would like to try and have this done automatically so if the user changes the quantity of a product, the final total changes etc).
If someone could help me out here I will be VERY greatful. This is something I would like to get working as this is going to be a component of Stage 2 of my auction script 9created from scratch (Stage 1 is working 95% perfect).
Here is the source code for the form:
The Javascript for "scarfiesoupshack.js":
And the css code for "scarfiesoupshack.css":
Many thanks in advance!
So this is my first real crack at Javascript, for this example I have tried to make an order form with Javascript that does the client-side validation of form fields. What I am wanting to do is when the user selects a "quantity" from the dropdown menu, the subtotal is automatically calculated and produced under a "subtotals" heading (for some reason, this heading is not displayed and that is most likely because of the Javascript & html hooks). This is done for each product listed in the form. Then finally I am wanting to add all the subtotals for each product to calculate the final total (ideally I would like to try and have this done automatically so if the user changes the quantity of a product, the final total changes etc).
If someone could help me out here I will be VERY greatful. This is something I would like to get working as this is going to be a component of Stage 2 of my auction script 9created from scratch (Stage 1 is working 95% perfect).
Here is the source code for the form:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="scarfiesoupshack.css">
<script src="scarfiesoupshack.js" type="text/javascript"></script>
<title>Scarfie Soup Shack</title>
</head>
<body>
<h1>We Make Soup (and sell it to you)</h1>
<p><img src="soup-here.jpg" alt="The Scarfie Soup Shack."></p>
<p>Our soup shack sits in the middle of Otago University quad - you
can't miss us.</p>
<p>If you tell us what you want, we'll put it in a
box with your name on it, ready to pick up in about 5 minutes. So why wait
out in the cold? Put in your order from the comfort of your own
office, then come on over and collect it from the shack.</p>
<h1>Place your order:</h1>
<form method="post" action="#" name="souporderform" id="souporderform" onsubmit="return validate()">
<table>
<tr><th>Item</th><th>Price</th><th>Quantity</th><th id="subtotal_header">Subtotal</th>
<tr>
<td><label for="pumpkin_quantity">Pumpkin Soup</label></td>
<td>$3<input type="hidden" id="pumpkin_price" value="3.00"></td>
<td align="center"><select id="pumpkin_quantity" name="pumpkin_quantity" size="1">
<option value="10">10</option>
<option value="9">9</option>
<option value="8">8</option>
<option value="7">7</option>
<option value="6">6</option>
<option value="5">5</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
<option value="0" selected>0</option>
</select>
</td>
<td id="pumpkin_subtotal"></td>
</tr>
<tr>
<td><label for="chicken_quantity">Chicken Soup</label></td>
<td>$3.50<input type="hidden" id="chicken_price" value="3.50"></td>
<td align="center"><select id="chicken_quantity" name="chicken_quantity" size="1">
<option value="10">10</option>
<option value="9">9</option>
<option value="8">8</option>
<option value="7">7</option>
<option value="6">6</option>
<option value="5">5</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
<option value="0" selected>0</option>
</select>
</td>
<td id="chicken_subtotal"></td>
</tr>
<tr>
<td><label for="clam_quantity">Clam Chowder</label></td>
<td>$4<input type="hidden" id="clam_price" value="4.00"></td>
<td align="center"><select id="clam_quantity" name="clam_quantity" size="1">
<option value="10">10</option>
<option value="9">9</option>
<option value="8">8</option>
<option value="7">7</option>
<option value="6">6</option>
<option value="5">5</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
<option value="0" selected>0</option>
</select>
</td>
<td id="clam_subtotal"></td>
</tr>
<tr>
<td><label for="harira_quantity">Harira</label></td>
<td>$3.50<input type="hidden" id="harira_price" value="3.50"></td>
<td align="center"><select id="harira_quantity" name="harira_quantity" size="1">
<option value="10">10</option>
<option value="9">9</option>
<option value="8">8</option>
<option value="7">7</option>
<option value="6">6</option>
<option value="5">5</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
<option value="0" selected>0</option>
</select>
</td>
<td id="harira_subtotal"></td>
</tr>
<tr id="tablefoot"><td>Total:</td><td colspan="2" id="totalerr"><td id="finaltotal"></td></tr>
</table>
<p><label for="yourname">Your name (so we can write it on the box):</label>
<input name="yourname" id="yourname" type="text">
<span id="nameerr"></span></p>
<p><label for="youremail">Your email address (we'll confirm your order):</label>
<input name="youremail" id="youremail" type="text">
<span id="emailerr"></span></p>
<div><button id="submit" type="submit">Place Order</button><button id="reset" type="reset">Clear Form</button></div>
</form>
</body>
Code:
// file scarfiesoupshack.js
var soups = ["pumpkin", "chicken", "clam", "harira"];
//get prices of soups
var pumpkin = document.getElementById("pumpkin_price");
var pumpkinprice = pumpkin.value;
var chicken = document.getElementById("chicken_price");
var chickenprice = chicken.value;
var clam = document.getElementById("clam_price");
var clamprice = clam.value;
var harira = document.getElementById("harira_price");
var hariraprice = harira.value;
//end get prices
function setup() {
var lastCol = document.getElementById("subtotal_header");
lastCol.style.display = "table-cell";
var tableFoot = document.getElementById("tablefoot");
tableFoot.style.display = "table-row";
var theForm = document.getElementById("souporderform");
var amounts = document.getElementsByTagName("select");
for(var i = 0; i < amounts.length; i++){
amounts[i].onchange = doTotals;
}
theForm.onsubmit = validate;
theForm.onreset = resetTotals;
}
function validate() {
var result = true;
var namefield = document.getElementById("yourname");
var nameerr = document.getElementById("nameerr");
var namefieldRE = /^[\w ]+$/;
var emailfield = document.getElementById("youremail");
var emailerr = document.getElementById("emailerr");
//email regex sourced from http://www.tizag.com/javascriptT/javascriptform.php
var emailfieldRE = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/ ;
if(!namefield.value.match(namefieldRE)){
nameerr.innerHTML = "Please fill in your name";
nameerr.style.color = "#ff0000";
result = false;
}
else{
nameerr.innerHTML = ""
result = true;
}
if(!emailfield.value.match(emailfieldRE)){
emailerr.innerHTML = "Please fill in your email";
emailerr.style.color = "#ff0000";
result = false;
}
else{
emailerr.innerHTML = "";
result = true;
}
return result;
}
function resetTotals() {
var total = 0.0;
for (soup in soups) {
var subtotal = soups[soup] + "_subtotal";
document.getElementById(subtotal).innerHTML = "0.00";
}
output = document.getElementById("finaltotal");
output.innerHTML = "0.00";
}
function doTotals() {
alert();
//var total = 0.0;
for (soup in soups) {
var pumpkinselectedvalue = document.souporderform.pumpkin_quantity.value;
var chickenselectedvalue = document.souporderform.chicken_quantity.value;
var clamselectedvalue = document.souporderform.clam_quantity.value;
var hariraselectedvalue = document.souporderform.harira_quantity.value;
document.getElementByID(soups[soup] + "_subtotal") = (soups[soup] + "price") * (soups[soup] + "selectedvalue");
var subtotal = soups[soup] + "_subtotal";
document.getElementById(subtotal).innerHTML = "0.00";
}
output = document.getElementById("finaltotal");
output.innerHTML = total.toFixed(2);
}
if (document.getElementById) {
window.onload = setup;
}
Code:
body {
font-family: sans-serif;
}
table {
border-spacing: 0;
}
th {
padding: 0.4em 1em;
border-bottom: solid black 2px;
}
td {
padding: 0.4em 1em;
}
#subtotal_header {
display: none;
}
#tablefoot {
display: none;
}
#tablefoot td {
border-top: solid black 2px;
border-bottom: solid black 2px;
}
button {
float: left;
margin: 1em 1em;
}