How would I create a form that calculates a total

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

How would I create a form that calculates a total

Post by someguyhere »

I'm planning out a project and can't seem to figure out the best way to go about it. I will have a form that will have several fields, some will be drop down menus and that form will allow someone to select various choices to build a particular package. That is the part I'm having trouble with. Once all of the selections have been made and the form is submitted, it will send a line by line price quote with a total at the end. It's easy to get the amounts or the info into that email, but how can I put them both? For example, lets use a rocketship as an example:

Engine

option 1 - Solid fuel ($1,000)
option 2 - Nuclear fuel ($5,000)
option 3 - Ion fuel ($10,000)

And then when the form is submitted, the line item for this would be something like:

Other item info and prices here
Engine: Nuclear fuel ($5,000)
Other item info and prices here

Total: $35,000

Hopefully my explaination makes sense.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How would I create a form that calculates a total

Post by pickle »

You said it was simple to get the amounts or the info into the email. I assume you've got different code to put both of those into the email body. Why not just run both those snippets so both pieces of information get put in the email body?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: How would I create a form that calculates a total

Post by someguyhere »

Because each drop down will have several options. I can assign a value to each option in the html, but it would either be the item info or the item price, not both as two seperate bits of info.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How would I create a form that calculates a total

Post by califdon »

Are you asking how to extract the dollar amount from a string, like "Nuclear fuel ($5,000)"? You could do it in several ways, like finding the substring that begins after the dollar sign, then terminating it before the closing quote:

Code: Select all

$x="Nuclear fuel ($5,000)";
$dollar=strpos($x,"$");
$quote=strpos($x,")");
$num=substr($x,$dollar + 1,$quote - $dollar);
Then you'd probably want to eliminate the comma. It all depends on how you're formatting the data.

You could do it, no doubt, also with regex. But I've never gotten proficient with regex, so I'll leave that to someone else.

But the better way to do it, I'd think, would be to handle the dollar amounts separately, as numbers, combining them with the rest of the description when you form the strings for display.
Post Reply