/    Sign up×
Community /Pin to ProfileBookmark

table to add up a total

I am a new guy here… Just found this site…

I am looking to have a table with prices and I want the total to be display at the bottom.
Here is the example

<SCRIPT TYPE=”text/javascript”>
<!–
function orderTotal(oform, prefix)
{
// set references to fields
var qty = oform[prefix + “_qty”];
var stHold = oform[prefix + “_
stHold”];
var price = oform[prefix + “_price”];
var stVis = oform[prefix + “_
stVis”];

// only bother if the field has contents
if (qty == “”)return;

// if the with is not a number (NaN)
// or is zero or less
// everything goes blank
if(isNaN(qty.value) || (qty.value <= 0))
{
qty.value = “”;
stHold.value = “”;
}

// else the field is a valid number, so calculate the
// total order cost and put that value in the
// hidden subtotal field
else
stHold.value = (Math.round(qty.value * price.value * 100))/100;

// call the routine which checks if the
// visible subtotal is correct
visTotal(oform, prefix);
}

// checks if the visible subtotal is correct
// ie, if it equals the hidden subtotal field
function visTotal(oform, prefix)
{
var stHold = oform[prefix + “_stHold”];
var stVis = oform[prefix + “_
stVis”];

if (stVis.value != stHold.value)
stVis.value = stHold.value;
}
// –>
</SCRIPT>

<FORM ACTION=”../cgi-bin/mycgi.pl”>
<TABLE BORDER CELLPADDING=3>
<!– table titles –>
<TR BGCOLOR=”#99CCFF”>
<TH>item</TD>
<TH>quantity</TD>
<TH>price</TD>
<TH>total</TD>
</TR>

<!– v-neck sweater –>
<TR BGCOLOR=”#FFFFCC”>
<TD>v-neck sweater</TD>
<TD><INPUT
TYPE=TEXT
NAME=”vn_qty”
SIZE=3
onChange=”orderTotal(this.form, ‘vn’)”
>
1</TD>
<TD><INPUT TYPE=HIDDEN NAME=”vn_price” VALUE=”33.95″>$33.95</TD>
<TD ALIGN=RIGHT>
<INPUT TYPE=HIDDEN NAME=”vn_stHold”>
<INPUT
TYPE=TEXT
NAME=”vn_stVis”
SIZE=10
onChange=”visTotal(this.form, ‘vn’)”
></TD>
</TR>

<!– JoAnn style blazer –>
<TR BGCOLOR=”#FFFFCC”>
<TD>JoAnn style blazer</TD>
<TD><INPUT
TYPE=TEXT
NAME=”ja_qty”
SIZE=3
onChange=”orderTotal(this.form, ‘ja’)”
></TD>
<TD><INPUT TYPE=HIDDEN NAME=”ja_price” VALUE=”99.95″>$99.95</TD>
<TD ALIGN=RIGHT>
<INPUT TYPE=HIDDEN NAME=”ja_stHold”>
<INPUT
TYPE=TEXT
NAME=”ja_stVis”
SIZE=10
onChange=”visTotal(this.form, ‘ja’)”
></TD>
</TR>

</TABLE>

<P><INPUT TYPE=SUBMIT VALUE=”submit”>
</FORM>

:rolleyes::rolleyes:

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@DasherJun 08.2012 — Maybe this should have been posted in the Javascript section.
Copy linkTweet thisAlerts:
@LutinFouJun 08.2012 — Is this what you are looking for (I have just made some modifications to your code)

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

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>HTML</title>

</head>

<body>


<SCRIPT TYPE="text/javascript">

<!--

function orderTotal(oform, prefix)

{

// set references to fields

var qty = oform[prefix + "_qty"];

var stHold = oform[prefix + "_
stHold"];

var price = oform[prefix + "_price"];

var stVis = oform[prefix + "_
stVis"];

// only bother if the field has contents

if (qty == "")return;

// if the with is not a number (NaN)

// or is zero or less

// everything goes blank

if(isNaN(qty.value) || (qty.value <= 0))

{

qty.value = "";

stHold.value = "";

}

// else the field is a valid number, so calculate the

// total order cost and put that value in the

// hidden subtotal field

else

stHold.value = (Math.round(qty.value * price.value * 100))/100;

// call the routine which checks if the

// visible subtotal is correct

visTotal(oform, prefix);

}

// checks if the visible subtotal is correct

// ie, if it equals the hidden subtotal field

function visTotal(oform, prefix)

{

var stHold = oform[prefix + "_stHold"];

var stVis = oform[prefix + "_
stVis"];

if (stVis.value != stHold.value)

stVis.value = stHold.value;

//get total price of v-neck sweater

var vSweatTotal=parseFloat(document.getElementById('vn_stVis').value);

//get total of blazer

var blazer=parseFloat(document.getElementById('ja_stVis').value);

//add the two values together

var sum=parseFloat(vSweatTotal)+(blazer);

//print the value to table

var myTotal=document.getElementById('myTotal');

myTotal.innerHTML=sum.toFixed(2);

}

// -->

</SCRIPT>

<FORM ACTION="../cgi-bin/mycgi.pl">

<TABLE BORDER CELLPADDING=3>

<!-- table titles -->

<TR BGCOLOR="#99CCFF">

<TH>item</TD>

<TH>quantity</TD>

<TH>price</TD>

<TH>total</TD>

</TR>

<!-- v-neck sweater -->

<TR BGCOLOR="#FFFFCC">

<TD>v-neck sweater</TD>

<TD><INPUT

TYPE=TEXT

NAME="vn_qty"

SIZE=3

onChange="orderTotal(this.form, 'vn')"
>
1</TD>

<TD><INPUT TYPE=HIDDEN NAME="vn_price" VALUE="33.95">$33.95</TD>

<TD ALIGN=RIGHT>

<INPUT TYPE=HIDDEN NAME="vn_stHold">

<INPUT

TYPE=TEXT

id="vn_stVis"

SIZE=10

onChange="visTotal(this.form, 'vn')"
></TD>

</TR>


<!-- JoAnn style blazer -->

<TR BGCOLOR="#FFFFCC">

<TD>JoAnn style blazer</TD>

<TD><INPUT

TYPE=TEXT

NAME="ja_qty"

SIZE=3

onChange="orderTotal(this.form, 'ja')"
></TD>

<TD><INPUT TYPE=HIDDEN NAME="ja_price" VALUE="99.95">$99.95</TD>

<TD ALIGN=RIGHT>

<INPUT TYPE=HIDDEN NAME="ja_stHold">

<INPUT

TYPE=TEXT

id="ja_stVis"

SIZE=10

onChange="visTotal(this.form, 'ja')"

></TD>

</TR>

<tr><td colspan="3"align="right">Order total</td><td><span id="myTotal"></span></td></tr>


</TABLE>

<P><INPUT TYPE=SUBMIT VALUE="submit">

</FORM>

</body>
</html>
Copy linkTweet thisAlerts:
@madoxnolaauthorJun 08.2012 — I guess I posted on the wrong forum, Sorry... My first time here...

Yes. this worked great.

Thank you guys...
Copy linkTweet thisAlerts:
@LutinFouJun 09.2012 — You're welcome, glad to be of help
×

Success!

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