/    Sign up×
Community /Pin to ProfileBookmark

Help Using JavaScript with forms

Hi,

I have to make a pizza ordering form and when I click the order button it should display the total price in a field. Here is what I have so far. I have no idea how to make it display in the field when I click on the order button. Please help.

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>

<!– Author: Hanley Chan
Date: March 17, 2004
Purpose: A program for ordering a pizza.
–>

<head>
<title>Ordering a Pizza</title>
<script language=”JavaScript” type=”text/javascript”>

function TotalQuantity()
{
var Quantity;
Quantity = document.form1.Quantity.value;
return Quantity;
}

function PriceSize()
{
var PriceSize;

if (document.form1.Size[0].checked)
{
PriceSize = 10;
}
if (document.form1.Size[1].checked)
{
PriceSize = 15;
}
if (document.form1.Size[2].checked)
{
PriceSize = 20;
}
return PriceSize;
}

function PriceTopping()
{
var NumTopping = 0;
var PriceTopping;

if (document.form1.Topping[0].checked)
{
NumTopping++;
}
if (document.form1.Topping[1].checked)
{
NumTopping++;
}
if (document.form1.Topping[2].checked)
{
NumTopping++;
}

PriceTopping = NumTopping * 0.3;
return PriceTopping;
}

function PriceTotal()
{
var Quantity;
var SizePrice;
var ToppingPrice;
var TotalPrice;

Quantity = TotalQuantity();
SizePrice = PriceSize();
ToppingPrice = PriceTopping();
TotalPrice = “$” + Quantity * (SizePrice + ToppingPrice) + “0”;

return TotalPrice;
}

</script>
</head>
<body>
<noscript>
<h2>You really need JavaScript to view this page!</h2>
</noscript>
<h1>Please fill in your pizza order in the form below:</h1>

<form name = “form1” action = “”>

Quantity: <br />
<input type = “text” name = “Quantity” size = “2” value = “1”> <br /><br />

Size: <br />
<input type = “radio” name = “Size” value = “Small” /> Small($10 each)
<input type = “radio” name = “Size” value = “Medium”/> Medium($15 each)
<input type = “radio” name = “Size” value = “Large” checked = “checked” /> Large($20 each) <br /><br />

Toppings($0.30 for each topping): <br />
<input type = “checkbox” name = “Topping” /> Pepperoni
<input type = “checkbox” name = “Topping” /> Pineapples
<input type = “checkbox” name = “Topping” /> Ham <br /><br />

<input type = “button” name = “Order” value = “Order” /><br />
Summary:
<input type = “text” name = “Summary” value = “$0.00” onblur = “value = PriceTotal()”/>

</form>

<address>
<a href=”http://validator.w3.org/check/referer”><img
src=”http://validator.w3.org/images/vxhtml10” height=”31″ width=”88″
alt=”Valid XHTML 1.0!” /></a>
</address>

</body>

</html>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@NedalsMar 17.2004 — Here's a couple of pointers to get you started:

You need an instruction to get your javascript to function, which you do here.

<input type="button" name="Order" value="Order" />

You have two choices.

1. Change the above type to 'submit' and include the following in the <form>.

onsubmit = "return javascript_function_name()"

Make sure that this function 'returns false' otherwise the form's 'action' will be carried out (which you don't want)

OR 2. Add the following to the <input> (as is).

onclick = "return javascript_function_name()"

and again 'return false'.

You also need to put the 'Total' in your 'summary' <input>

<input type="text" name="Summary" value="" />

Use ...document.forms[0].Summary.value = your_total... to put it there.

That you get you started on the right track. ?
×

Success!

Help @hanleychan 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.7,
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,
)...