/    Sign up×
Community /Pin to ProfileBookmark

Order form problem (Assignment)

Hi, i am new to javascript and i am doing an order form for my assignment.

[B]The order form basic structure is this:[/B]
[B]quantity01 * 100 = subtotal01
quantity02 *
100 = subtotal02
grand total = subtotal01 + subtotal02 [/B]

I have done the first two parts but when i did the third one i got stucked.
[B]The problem is just the result of subtotal01 + subtotal02 resulting subtotal01subtotal02[/B]. look like the number suddenly changed to string.

so for example; [B]200 + 200 = 200200 instead of giving me 400[/B]. Please help me

The code is below: Thank you

<HEAD>
<script type=”text/javascript”>
<!– Begin

function startCalc(){
interval = setInterval(“calc()”,1);
}
function calc(){
one = document.orderform01.quantity01.value;
two = 100
three = document.orderform02.quantity02.value;
document.orderform01.subtotal01.value = (one) * (two);
document.orderform02.subtotal02.value = (three) *
(two);

price01 = document.orderform01.subtotal01.value
price02 = document.orderform02.subtotal02.value
grandtot = ((price01) +(price02))

document.orderform03.grand_total.value = grandtot
}
function stopCalc(){
clearInterval(interval);
}

</script>

</HEAD>

<BODY>

<form name=”orderform01″>

<table border=”1″>
<tr>
<td> <input type=text name=”quantity01″ value=”” onFocus=”startCalc();” onBlur=”stopCalc();”> </td>
<td width=”128px”> 100 </td>
<td> <input type=text name=”subtotal01″> </td>
</tr>
</form>

<form name=”orderform02″>
<table border=”1″>
<tr>
<td> <input type=text name=”quantity02″ value=”” onFocus=”startCalc();” onBlur=”stopCalc();”> </td>
<td width=”128px”> 100 </td>
<td> <input type=text name=”subtotal02″> </td>
</tr>
</form>

<form name=”orderform03″>
<table border=”1″>
<tr>
<td width=”280px”> Total </td>
<td width=”146px”> <input type=text name=”grand_total” value=”” /> </td>
</tr>
</table>
</form>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@CharlesSep 06.2006 — In JavaScript the additive operator, "+", concatenates strings. You need to cast your values each as a Number:grandTotal = Number (subTotal01) + Number (subTotal02)And do note, those trailling "/" characters in empty element tags, [font=monospace]<input [b]/[/b]>[/font], they're from XHTML and have no business in HTML. In HTML they have a different, obscure meaning.
Copy linkTweet thisAlerts:
@gunmonauthorSep 07.2006 — ? many thanks i am doing it in xhtml actually, should have told you anyway thanks ?
Copy linkTweet thisAlerts:
@gunmonauthorSep 07.2006 — ?

have tried what you said, whenever i put :

[B]grandtot = number (price01) + number (price02)[/B]

the text box of the grandtot stop working. ? .

[B]i just think because price 01 and price 02 are variables so the function of number is not working for them.



I have tried using parseInt and parseFloat, they are not working as well.[/B]


any suggestion?

nb: thi is in xhtml actually not html
Copy linkTweet thisAlerts:
@KravvitzSep 07.2006 — JavaScript is case-sensitive. number() is not the same as Number()
Copy linkTweet thisAlerts:
@gunmonauthorSep 07.2006 — Hi i have tried your suggestion as well. ?

It is starting to work again but it wont sum up the result rather it combines the result.

[B]grandtot = Number(price01) + Number(price02)[/B]

whatever result are the price01 and price 02, it becomes grandtot = price01price02

so grandtot=200200 instead of giving me 400.


[B]here i show you the complete xhtml page:

[/B]


<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">


<head>

<script type="text/javascript">

<!-- script hiding

function startCalc(){

interval = setInterval("calc()",1);

}

function calc(){

one = document.orderform01.quantity01.value;

two = 100

three = document.orderform02.quantity02.value;

document.orderform01.subtotal01.value = (one) * (two);

document.orderform02.subtotal02.value = (three) *
(two);

price01 = document.orderform01.subtotal01.value

price02 = document.orderform02.subtotal02.value

grandtot = Number(price01) + Number(price02)

document.orderform03.grand_total.value = grandtot;

}

function stopCalc(){

clearInterval(interval);

}



// end script hiding -->

</script>



</head>


<body>

<form name="orderform01">

<table border="1">

<tr>

<td> <input type=text id="quantity01" value="" onfocus="startCalc();" onblur="stopCalc();"> </td>

<td width="128px"> 100 </td>

<td> <input type=text id="subtotal01"> </td>

</tr>

</form>

<form name="orderform02">

<table border="1">

<tr>

<td> <input type=text id="quantity02" value="" onfocus="startCalc();" onblur="stopCalc();"> </td>

<td width="128px"> 100 </td>

<td> <input type=text id="subtotal02"> </td>

</tr>

</form>

<form name="orderform03">

<table border="1">

<tr>

<td width="280px"> Total </td>

<td width="146px"> <input type=text id="grand_total" value="" /> </td>

</tr>

</table>

</form>

</body>

</html>
Copy linkTweet thisAlerts:
@KravvitzSep 07.2006 — Your code works fine for me in FF1.5 and IE6.
×

Success!

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