Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to create a js shopping page

Status
Not open for further replies.

lahddah

Programmer
Joined
Jan 2, 2003
Messages
109
Location
US
Hello, I'm going to add a link below - I have tried putting together a couple of different things to try to get this page to function correctly. All I want it to do is keep a running total based on user input. Then, depending on what the total dollar amt is, either add the $7.50 shipping or not, then add the state tax. I can get the running total, but cannot figure out how to calculate the rest of the items. Thanks, in advance, to anyone who wouldn't mind taking the time to peruse this code!

----

Thanks! ~ lahddah
 
lahddah,

Feel free to steal my code at

It keeps a running total, and calculates shipping as item count changes.

Cheers,

Edward "Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
by the way...

Keep in mind that, as fancy and nifty as your JavaScript gets, you should still have some sort of server-side application verify everything. An unscrupulous person could copy your JavaScript, monkey with the values, and then run it locally. It would still send out order data to your server, but with, shall we say, the "programmer's discount"?

That's my only warning about such things (and it's a good one!)

Cheers,

Edward "Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
Thanks, Edward -

I am trying, though, to just 'tweak' my existing code rather than going through and changing the whole page (field/form names, prices, images, etc.). I think I've spend WAY too much time on this already, so I was just looking for a 'quick fix'. Your code is great, and I wish I would've seen it before I started this project, but now I'm to the point where what I do from here on out is on 'my own time' and cannot be charged to the client.

The problem, I think (or one of them) with my current code is that I am trying to do calculations on a string - and it's coming up as NAN. I can't figure out why just about everything else is working until I try to factor in the shipping and tax fields.

thanks, again, Edward. ~ lahddah
 
Well, JavaScript's pretty loosely typed. If you're trying to do calculations on a string, yeah, it's going to be NaN, but you can coerce a string value to an Integer or a Float using parseInt(); or parseFloat();

There are a few caveats, though. For example, parseInt stops parsing the string when it hits a non-numeric. So parseInt(3.14 15927) will produce a numeric value of 3.14. parseFloat doesn't allow you to specify decimal places, so you don't have that control.

But if you're working with US dollars and you won't be seeing fractional cents, just multiply everything by a hundred and do integer calculations and then divide back by a hundred at the end.

Here are some web pages that might be helpful:
and

Good luck!

Edward "Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top