View Full Version : Sending a form to another page ???

11-01-2007, 11:34 PM
OK, here is the code I started with initially:
it's just a very simple form where the user gets to pick a topping

<form action="order.php" method="post">
Please Pick your topping:
<p><input type="radio" name="topping" value="cheese" /> cheese <br>
<input type="radio" name="topping" value="bacon" /> bacon
<input type="submit" />

and my order.php file looks basically like this:



<p>You choose: <?php echo $_POST["topping"]; ?>"


so if the user choose bacon, the php form would display: You choose: bacon


that was easy enough. (php would output what was in the "value" field of the form. i.e. if the value was cheese, it would display cheese. etc

now I have beefed up the options a user can select, and added a javascript to add up everything they have selected, because I have added options that are priced differently...
but the way the javascript works, it is using the "value" field in the form as the amount of the item,

<input type="radio" value="1.00" name="topping" onclick="CalculateTotal(this);">

so using my original php script to "pass" the name of the field chosen to the next page, it now passes the amount cause of the javascript i'm using to calculate the total

is there any way to pass the name of the item on, (and also the price) ??

11-02-2007, 12:16 AM
What I'd probably do off the cuff is create a hidden form element for each option where the price is stored. My Javascript calculations would then use that hidden element to get the price and it will also be passed to PHP.

11-02-2007, 12:17 AM
It would be better if you kept the price in an array (or database) and simply looked it up when you need it. After all, the price associated with each ingredient changes over time. I hope you are not hard coding them throughout the code.

But if you still want to do it the way you have described, you can include both in the value -

<input type="radio" value="cheese|1.00" name="topping" onclick="CalculateTotal(this);">You can then explode the value string on the "|" character to get the two pieces of information.

Also, any calculations you do in javascript on the client side are useful for display purposes only and cannot be trusted when the data is submitted to the server. You need to calculate any actual pricing on the server.

Edit: Either I type really fast (unlikely) or I started this before Fumigator's post.

11-02-2007, 12:34 AM
CFM's method is better, I agree totally-- use a database to store and retrieve pricing etc.

My method would be difficult to maintain.

11-02-2007, 12:40 AM
This sounds a little bit like a homework assignment...
we don't do homework. But, the database idea is good,
and if you use PHP sessions, you could keep track of their
selections without using hidden form values. (sessions or cookies).

I guess none of us are sure how many items there are.

If this is homework, you can post questions pertaining to your
PHP script ... something that you wrote, not us.

11-02-2007, 01:39 AM
nope. not a homework assignment
but I'm just a super newbie to programming, so using a database to store stuff, and sessions/cookies is a little over my head :(

so if I make a hidden form, like this:

<p><input type="radio" name="topping" value="cheese"><input type="hidden" value="1.00"> cheese

how do I call that hidden value so I can display: cheese and 1.00

I call "cheese" like this:

<p>You choose: <?php echo $_POST["topping"]; ?>"

but i'm unsure of how to call something in the hidden form

and i think this is kinda turning into an html question :confused:

11-02-2007, 01:40 AM
You grab the hidden value the same way, by the name. You forgot to name your hidden field.