Connecting Tech Pros Worldwide Forums | Help | Site Map

drop-down menu and conditional response on page 2

djsjm's Avatar
Newbie
 
Join Date: Nov 2008
Location: USA
Posts: 23
#1: Nov 1 '08
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's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#2: Nov 1 '08

re: drop-down menu and conditional response on page 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:
Expand|Select|Wrap|Line Numbers
  1. <form action="process.php" method="post">
  2.   <input type="text" name="someValue" /><br />
  3.   <input type="submit" />
  4. </form>
  5.  
Once submitted, this code in the "process.php" file would show you what the user typed in the "someValue" textbox:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   echo $_POST['someValue'];
  3. ?>
  4.  
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.
djsjm's Avatar
Newbie
 
Join Date: Nov 2008
Location: USA
Posts: 23
#3: Nov 1 '08

re: drop-down menu and conditional response on page 2


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.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#4: Nov 2 '08

re: drop-down menu and conditional response on page 2


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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if($_POST['pageSelection'] == "home") {
  3.   header("Location: index.php");
  4. }
  5. else if($_POST['pageSelection'] == "login") {
  6.   header("Location: memberLogin.php");
  7. }
  8. else {
  9.   header("Location: pageSelectionForm.html");
  10. }
  11. ?>
  12.  
This would simply check the value of a "pageSelection" 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 "pageSelectionForm.html")

Some people might recommend the switch statement rather than the if statements, but I have personally never seen the point of it.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#5: Nov 2 '08

re: drop-down menu and conditional response on page 2


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.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#6: Nov 2 '08

re: drop-down menu and conditional response on page 2


Quote:

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().
djsjm's Avatar
Newbie
 
Join Date: Nov 2008
Location: USA
Posts: 23
#7: Nov 3 '08

re: drop-down menu and conditional response on page 2


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...
Reply