Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Sending a form to another page ???

    OK, here is the code I started with initially:
    it's just a very simple form where the user gets to pick a topping
    Code:
    <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
    </p>
    <input type="submit" />
    </form>
    and my order.php file looks basically like this:
    Code:
    <html>
    
    <head>
    </head>
    <body>
    
    <p>You choose: <?php echo $_POST["topping"]; ?>" 
    </p>
    </body>
    
    </html>
    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,
    like:
    Code:
    <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) ??

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,111
    Thanks
    2
    Thanked 326 Times in 318 Posts
    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 -
    PHP Code:
    <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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    CFM's method is better, I agree totally-- use a database to store and retrieve pricing etc.

    My method would be difficult to maintain.

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    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.

  • #6
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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:
    Code:
    <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:
    Code:
    <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

  • #7
    New Coder
    Join Date
    Oct 2007
    Posts
    84
    Thanks
    0
    Thanked 8 Times in 8 Posts
    You grab the hidden value the same way, by the name. You forgot to name your hidden field.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •