how to navigate page using combolist

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jalazmi
Forum Newbie
Posts: 2
Joined: Sat Aug 22, 2009 3:15 pm

how to navigate page using combolist

Post by jalazmi »

hi everyone,

i`m new in php language..

what i need is.. i have combolist with 9 options.. if user chose any option i need to navigate them to page that related to what he chose

e.g: if he chose option 1 and press submit then browser navigate to page related to option 1 and so on so far

so how to do this..?

this is where i stop in my code:

Code: Select all

<html>
<body>
<form action="list.php" method="post">
<p>Select your stock<br>
<select name="select">
<option value="Option 1" selected>Bank Sector</option>
<option value="Option 2">----------</option>
<option value="Option 3">NBK</option>
<option value="Option 4">GBK</option>
<option value="Option 5">CBK</option>
<option value="Option 6">ABK</option>
<option value="Option 7">BKME</option>
<option value="Option 8">KIB</option>
<option value="Option 9">BURG</option>
<option value="Option 10">KFIN</option>     
<option value="Option 11">BOUBYAN</option>
</select>
<input type="submit" />
</form>
</p>
</body>
</html>
 

Code: Select all

<html>
<body>
<?php 
                if ($_POST['select'] == "Option 3"){
                    [color=#BF0000]what to write here so i can navigate to other page[/color]
                }
?>
                    
</body>
</html>
 



i hope you understand what i mean
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how to navigate page using combolist

Post by califdon »

If your script (the second one) has NOT sent ANYTHING yet to the browser, you can send a redirect header, like this:

Code: Select all

<?php
header('Location: http://www.example.com/');
but that If is very important. In your example, it must be done before even the lines <html> and <body> and there cannot even be a blank line before the <?php line. If you send anything before the header, you will receive an error message and it will not work. Read this: http://us2.php.net/manual/en/function.header.php.

If you cannot arrange to do this, then you can send Javascript, and if the browser has not disabled Javascript, it will send the browser to a new URL, like:

Code: Select all

echo "<script type='text/javascript'>";
echo "document.location='http://www.example.com/' </script>";
tabutcher
Forum Newbie
Posts: 20
Joined: Fri Aug 21, 2009 7:10 am

Re: how to navigate page using combolist

Post by tabutcher »

This is not a solution but want to point out that I think you might need to check for when the button is clicked and assign the option selected to a variable. It would be great to see this problem solved as i would also like to know. :D
For Example:

Code: Select all

<?php if((isset($_POST["submit"];)) ?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how to navigate page using combolist

Post by califdon »

Ordinarily there is no need to check for the submit button. And I just presented 2 solutions.

It would be appropriate to use PHP's switch syntax rather than If syntax, and if it were me, I'd use simple numbers as the option values, but his syntax would work as shown. Another variation would be to use the entire URL as the option value, eliminating the branching entirely:

Code: Select all

<select name="myselect">  // avoid using key words as names!
<option value="http://www.NBK.com/" selected>NBK</option>
<option value="http://www.GBK.com/">GBK</option>
... etc.
then in the action script:

Code: Select all

$dest=$_POST['myselect'];
header("Location: '$dest'");
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: how to navigate page using combolist

Post by jackpf »

Normally I just use javascript. Like this:

Code: Select all

 <select onchange="window.location = this.value;"><option value="just a placeholder" style="font-style: italic;">Choose a URL...</option><option value="http://someurl.com">Someurl</option><option value="http://anotherurl.com">anotherurl</option></select> 
You could use the onclick event of the options...but IE seems to ignore them for some reason.
jalazmi
Forum Newbie
Posts: 2
Joined: Sat Aug 22, 2009 3:15 pm

Re: how to navigate page using combolist

Post by jalazmi »

califdon wrote:Ordinarily there is no need to check for the submit button. And I just presented 2 solutions.

It would be appropriate to use PHP's switch syntax rather than If syntax, and if it were me, I'd use simple numbers as the option values, but his syntax would work as shown. Another variation would be to use the entire URL as the option value, eliminating the branching entirely:

Code: Select all

<select name="myselect">  // avoid using key words as names!
<option value="http://www.NBK.com/" selected>NBK</option>
<option value="http://www.GBK.com/">GBK</option>
... etc.
then in the action script:

Code: Select all

$dest=$_POST['myselect'];
header("Location: '$dest'");
thx for your help..

i try it but dose`t work...!!

i will use javascript with onchange .. i think it simple than php

thanks jackpf, califdon and tabutcher for your time and your help
Post Reply