form calculator?

They have: 45 posts

Joined: Jan 2000

I have been looking for some script that will tally up the total of a form after a customer is finished ordering and clicks a "total" button (or something similiar).
A short version of the HTML form is below. If the customer clicks the checkbox for both widgets below, and marks one quantity for each, I want them to be able to click a "total" button to see the total cost (50$).

<FORM ACTION="***" METHOD="POST">

Name:<INPUT TYPE="text" NAME="Name" SIZE=39 MAXLENGTH=60>

<INPUT TYPE="checkbox" NAME="widget" VALUE="YES">widget - $25.00 each
Qty.<INPUT TYPE="text" NAME="Qty." SIZE=1 MAXLENGTH=1>

<INPUT TYPE="checkbox" NAME="widget" VALUE="YES">widget - $25.00 each
Qty.<INPUT TYPE="text" NAME="Qty." SIZE=1 MAXLENGTH=1>

I've looked all over and tried to manipulate the code to a calculator, but have had no luck so far.It would be nice if the code was multi-browser friendly.

Does anyone have a clue ????

Thanks! <[email protected]>

------------------

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Just use Javascript to access the quantity text fields assign them to varialbes, add them up, and output ther result in a blank text field labelled "total." It would look something like this:

(I'm using qty1 and qty2 to separate the two widgets in the calculations, so for this to work your form fields would have to be labelled accordingly)

function calculate() {

var widget1= eval(document.form.qty1.value);
var widget2= eval(document.form.qty2.value);

var total = (widget1*25) + (widget2*25);

document.form.total.value = total;

}

Just call the function from onSubmit in the submit button. I hope this works for you. I'm not a Javascript guru by any means, but I've been working on something similar to this recently and it works!! (yeah, I'm proud)

Here's an article about accessing and validating forms - it might help
http://wsabstract.com/javatutors/form5.shtml

They have: 297 posts

Joined: Apr 1999

The eval is obsolete.

Malte

------------------
Malte Ubl - www.Boardzilla.org
Communication: public<->programmers
of the Boardzilla BB

They have: 45 posts

Joined: Jan 2000

Thanks Megs and Malte!

I don't have it down exactly yet, but I'll get it. I've only been coding in HTML a few weeks now. JAVASCRIPT is next, I think. I made the practice form below until I get it right.

Malte, when you said "eval" is obsolete, does that mean that browsers are going to stop recognizing it? It works on my version of Netscape (4 or 5?).

Thanks again.

<form name="example">

<CENTER>
WIDGET<INPUT TYPE="checkbox" NAME="WIDGET" VALUE="YES">
Qty.<INPUT TYPE="text" NAME="Qty." SIZE=1MAXLENGTH=1>
      
WIDGET<INPUT TYPE="checkbox" NAME="WIDGET" VALUE="YES">
Qty.<INPUT TYPE="text" NAME="Qty." SIZE=1 MAXLENGTH=1>
<BR><BR><BR><BR>

<input type="text" size="20" name="calculator">

<input type="button" name="B1" value="Calculate"
onclick="cal()">

<input type="reset" name="B2" value="Reset">

Answer:<input type="text" size="20" name="answer">
</form>

<script>
function cal()
{
document.example.answer.value=
eval(document.example.calculator.value)
}
</script>
</CENTER>

They have: 297 posts

Joined: Apr 1999

It works with and without the eval.

You only have to eval() an expression if it contains a string which is supposed to executed as an expression.

Malte

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi Yaeger,

You might want to see the "Orderform" script at my site; it uses arrays for the 'products' and the "with" command for that easy-on-the-fingers effect. Also comes with explanations.

Vinny GrassBlade: cut&paste javascript

Where the world once stood
the blades of grass cut me still

They have: 45 posts

Joined: Jan 2000

Thanks guys.

I seen your order form, Vincent. I'm messing around with the code and Megs's code. This javascript is making more sense.

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi

>This javascript is making more sense.

Or as j,p,g&r said: "Things are getting better all the time"

vinny

They have: 45 posts

Joined: Jan 2000

Vinny,

That orderform at Grassblade is definitely the idea. There's a long one at:
http://www.coolnerds.com/jscript/ofinfo.htm

[This message has been edited by syaegerii (edited 31 January 2000).]

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.