Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Page navigation based on a drop down 2

Status
Not open for further replies.

Kalisto

Programmer
Feb 18, 2003
997
GB
I have inherited a php site, which I'll admit is not something I know much about.

I can understand most of what is present, but I have been asked if I can put a drop down list in with different countries in it (Done that bit).

Now, on selecting a country and pressing an ok button, I need to navigate to a different page which is specific to the selected country.

Is that something that can be done in the PHP Page (i.e. Post back to it's self, look to see if a value has been selected from the drop down list, if it has then work out the url and redirect, if not show an error message)

or should I be looking to do this client side with javascript ?

My instinct tells me its the firsto option I need, but before I spend too much time on this I want to make sure Im right.

Any hints on how to do it, if Im right / wrong, or other better suggestions are more than welcome :)

K
 
Sure it can be done with PHP.

Surround the dropdown with a form, and either have JS submit it automatically upon selecting an option, or just have a submit button as you said than can be pressed after choosing an option.

The form should be submitted either t itself or even to an intermediate page that does the checking and redirects accordingly, either way the code is the same.

Code:
if(isset($_POST['nameofdropdown']){ [green]check to see if the dropdown has been used, [/green]

[green]use value from drop down to determine location [/green]
header("location:otherpage.php"); [green]\\Issue header redirect to send to the determined page[/green]
}





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Aside from vacunita's suggestion, you can also JS for this sort of thing (as you mentioned). The question I have is if you need to carry additional information to the re-directed page or just go there?

If you just need to jump to a page a simple onchange element will do
Code:
<select id="country" onchange="window.location.href=this.value">
<option value="[URL unfurl="true"]http://www.mysite.com/us/">USA</option>[/URL]
<option value="[URL unfurl="true"]http://www.mysite.com/br/">Brazil</option>[/URL]
<option value="[URL unfurl="true"]http://www.mysite.com/en/">England</option>[/URL]
</select>

Now, if you need to port over a whole lot of data with you, submitting the form may be necessary, then change your element to
<select id="country" onchange="document.formname.submit();">
<option value="<option value="<option value="</select>
[/code]

If I am not mistaken, both of these will make your drop down list jump out of your existing page!

Happy jumping!!!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Thanks, I got both solutions to work for me, just so I could learn.

K
 
It's a good idea to understand how both JS and PHP methods work. Sometimes people turn off JS and I suppose it's a business decision if you want to trade with these people or not. With the rise of ajax I suspect less and less people will switch of JS as time goes on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top