For Programmers: Free Programming Magazines  


Home > Archive > VBScript > September 2004 > ASP - Calculating and displaying totals









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author ASP - Calculating and displaying totals
Linger1974

2004-09-27, 8:55 am

Hi all,

It's not often I need to call on some of you for problems, but I'm in new
territory with this page I'm working on. I've created a sample "menu" on a
page and have some drop down menus for the entree, appetizer, etc and I wish
to display a running total at the bottom for everything that's selected. In
detail, I wish to have 3 fields: Subtotal, Taxes, Total.

I need some direction on how to proceed, since I'd like the values of these
fields updated when anything is selected / removed from the drop down menus.
I've heard it can be done with Javascript, though I'd like to stick with an
ASP solution, if one exists.

Thanks for any and all replies! MUCH appreciated.
McKirahan

2004-09-27, 8:55 am

"Linger1974" <Linger1974@discussions.microsoft.com> wrote in message
news:BEE19CA0-9826-4824-96D7-FF1C3932A8F6@microsoft.com...
> Hi all,
>
> It's not often I need to call on some of you for problems, but I'm in new
> territory with this page I'm working on. I've created a sample "menu" on

a
> page and have some drop down menus for the entree, appetizer, etc and I

wish
> to display a running total at the bottom for everything that's selected.

In
> detail, I wish to have 3 fields: Subtotal, Taxes, Total.
>
> I need some direction on how to proceed, since I'd like the values of

these
> fields updated when anything is selected / removed from the drop down

menus.
> I've heard it can be done with Javascript, though I'd like to stick with

an
> ASP solution, if one exists.
>
> Thanks for any and all replies! MUCH appreciated.


A JavaScript client-side solution is my recommendation.

An ASP solution would require a round-trip to the server on each onChange()
which would be slower and very noticable to the user. Also, you'd have to
capture (probably via Session variables) then restore the current values of
all form fields.

Why must you have an ASP (server-side) solution?



Linger1974

2004-09-27, 3:55 pm

It's only because I'm much better at ASP than Javascript... ;) I know this
is the wrong forum for it, but my inevitable next question is...

Anyone have any samples I could check out for this javascript solution? I
haven't been able to find any on the weeb, though I doubt I've been using the
right search terms since I'm not sure what this is called. :)

Thanks for your thoughts, McK!

"McKirahan" wrote:

> "Linger1974" <Linger1974@discussions.microsoft.com> wrote in message
> news:BEE19CA0-9826-4824-96D7-FF1C3932A8F6@microsoft.com...
> a
> wish
> In
> these
> menus.
> an
>
> A JavaScript client-side solution is my recommendation.
>
> An ASP solution would require a round-trip to the server on each onChange()
> which would be slower and very noticable to the user. Also, you'd have to
> capture (probably via Session variables) then restore the current values of
> all form fields.
>
> Why must you have an ASP (server-side) solution?
>
>
>
>

Evertjan.

2004-09-27, 3:55 pm

= Utf-8?B?TGluZ2VyMTk3NA==?= wrote on 27 sep 2004 in
microsoft.public.scripting.vbscript :
> It's only because I'm much better at ASP than Javascript... ;) I
> know this is the wrong forum for it, but my inevitable next question
> is...


ASP is a serverside platform that accomodates
both VBscript and J[ava]script.

So your question is somewhat incomprehensable.

> Anyone have any samples I could check out for this javascript
> solution? I haven't been able to find any on the weeb, though I doubt
> I've been using the right search terms since I'm not sure what this is
> called. :)


All VBScript under ASP can also be written in J[ava]script with the
same functionality.

I know of only one function in J[ava]script
that is not possible in VBscript.

================

If you are using the term ASP for serverside and Javascript for
clientside coding, you are seeing but part of the picture.

In IE VBscript can also be clientside, btw.

================

And yes, if you have clientside coding questions, this definitely is the
wrong NG, dear = Utf-8?B?TGluZ2VyMTk3NA==?=.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

McKirahan

2004-09-27, 3:55 pm

"Linger1974" <Linger1974@discussions.microsoft.com> wrote in message
news:6AB52A55-D016-451B-ABD1-85F5B5E13FB0@microsoft.com...
> It's only because I'm much better at ASP than Javascript... ;) I know

this
> is the wrong forum for it, but my inevitable next question is...
>
> Anyone have any samples I could check out for this javascript solution? I
> haven't been able to find any on the weeb, though I doubt I've been using

the
> right search terms since I'm not sure what this is called. :)
>
> Thanks for your thoughts, McK!


[snip]

This should get you started.

Save and test "as-is"; watch for word-wrap.

<html>
<head>
<title>foodmenu.htm</title>
<script type="text/javascript">
var asub;
var taxs = .05;
function changed() {
ords = "";
asub = 0;
var form = document.form1;
changer(form.appetizer.options[form.appetizer.selectedIndex].value);
changer(form.entree.options[form.entree.selectedIndex].value);
}
function changer(what) {
var item = what.split("$");
asub += parseInt(item[1],10);
var atax = asub * taxs;
var atot = asub + atax;
document.form1.sub.value = dollars(asub);
document.form1.tax.value = dollars(atax);
document.form1.tot.value = dollars(atot);
if (ords != "0.00") {
ords += "\n" + dollars(parseInt(item[1],10)) + " = " + item[0];
}
}
function dollars(what) {
var amtx = what + "";
if (amtx.indexOf(".") < 0) amtx += ".00";
var amts = amtx.split(".");
if (amts[1].length == 1) amts[1] += "0";
if (amts[1].length > 2) amts[1] = amts[1].substr(0,2);
var valu = amts[0] + "." + amts[1];
return "$" + valu;
}
function ordered(that) {
ords += "\n" + document.form1.sub.value + " = Subtotal";
ords += "\n" + "-------------";
ords += "\n" + document.form1.tax.value + " = " + (taxs*100) + "% Tax";
ords += "\n" + "=============";
ords += "\n" + document.form1.tot.value + " = Total";
document.write("<pre>" + ords + "</pre>");
return false;
}
</script>
</head>
<body>
<form name="form1" onsubmit="return ordered()">
<b>Food Menu</b>
<br><br>
<select name="appetizer" onchange="changed()">
<option value="$0.00">
<option value="Soup$1.00">Soup
<option value="Salad$2.00">Salad
</select>
<br>
<select name="entree" onchange="changed()">
<option value="$0.00">
<option value="Fish$5.00">Fish
<option value="Steak$10.00">Steak
</select>
<br><input type="text" name="sub" size="7" value="0"
style="text-align:right"> Subtotal
<br><input type="text" name="tax" size="7" value="0"
style="text-align:right"> Tax
<br><input type="text" name="tot" size="7" value="0"
style="text-align:right"> Total
<br><input type="submit" value=" Order! ">
</form>
</body>
</html>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com