drop-down menu and conditional response on page 2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djsjm
    New Member
    • Nov 2008
    • 23

    #1

    drop-down menu and conditional response on page 2

    Hi there... total newbie here... just typed out a beautiful question and then it sent me to log in and my question has just joined the stash of lost socks from washing machines. Start over.

    I'm trying to learn php to make 3 pages of my site functional. I have lots of questions... but I'll narrow it down to just one for this post. Let me also say that I'm a do-it-yourselfer and like to find the answers myself... and if there was a tracker on google search, you'd see I've been busy ramming my head against a wall for almost two weeks now.

    I have a pull down menu with three choices, as part of a form on page 1.

    I would like the text displayed on page 2 to reflect what the user's choice was on page 1.

    Will someone point me in the right direction? What do I need to study... sessions... includes... if, ifelse.......

    I'm learning a lot, but not what I need for this particular task.

    Thanks in advance.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    I think you may find this article helpful. It's about how to use HTML forms to pass your data to PHP.

    The short version of that would be:

    If you have a HTML <form>, somewhat like this:
    [code=html]
    <form action="process .php" method="post">
    <input type="text" name="someValue " /><br />
    <input type="submit" />
    </form>
    [/code]
    Once submitted, this code in the "process.ph p" file would show you what the user typed in the "someValue" textbox:
    [code=php]
    <?php
    echo $_POST['someValue'];
    ?>
    [/code]
    All the values the user chose for all <input>, <textarea> and <select> elements inside the <form> element are passed to the $_POST super-global as an array, using the "name" attribute of the element as the key.

    Comment

    • djsjm
      New Member
      • Nov 2008
      • 23

      #3
      Hi Atli,

      Thanks for your quick response. I had already read the article that you linked to, as well as about 100 other tutorials online saying the same thing. What I need is to go beyond merely displaying what the choice was.

      I'd like the page content to be different based on that choice. If there was a way to put an if (or something) into the action element of the form, then I could just direct the user to a specific page based on their choice... rather than trying to change the content of one page.

      Does that make sense? I know I don't have the lingo and everything right... I didn't even know php existed until a couple of weeks ago.

      Thanks again.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Ok I see.

        You could use the header function to re-direct the client to another page based on the value that was passed to your script.

        A simple if statement would be all that you needed. (Although there are a lot of alternatives to that.)

        Something as simple as this might work:
        [code=php]
        <?php
        if($_POST['pageSelection'] == "home") {
        header("Locatio n: index.php");
        }
        else if($_POST['pageSelection'] == "login") {
        header("Locatio n: memberLogin.php ");
        }
        else {
        header("Locatio n: pageSelectionFo rm.html");
        }
        ?>
        [/code]
        This would simply check the value of a "pageSelect ion" variable, sent via a <form>. It would redirect to specific pages if it was past "home" or "login", or simple go back to the form if anything else is sent. (The page with the form being "pageSelectionF orm.html")

        Some people might recommend the switch statement rather than the if statements, but I have personally never seen the point of it.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          In C#.NET switch() is generally considered to be faster but less flexible than if(). I'm not sure if the speed difference is true with PHP, though.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Originally posted by Markus
            In C#.NET switch() is generally considered to be faster but less flexible than if(). I'm not sure if the speed difference is true with PHP, though.
            That doesn't seem to be the case with PHP.
            I've just done a few simple tests and if() appears to be like 3% faster than switch().

            Comment

            • djsjm
              New Member
              • Nov 2008
              • 23

              #7
              Thanks for the advice! I did end up getting it to work the other night using "include". Just getting back to work now after having the flu. At least I was the one with the virus and not my pc. =)

              Now for a new post with a new question...

              Comment

              Working...