Redirect options list not working

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
leegee3
Forum Newbie
Posts: 10
Joined: Thu Mar 29, 2007 7:28 am

Redirect options list not working

Post by leegee3 »

Again, I am a beginner at php and html so please go easy on me


I have a php file that has the following code...

Code: Select all

<?php

if($location != null)
{
	header("Location:$location");
	exit();
}

?>

<html><head><title> Redirect </title></head>
<body>

Please select a site:

<form action="<?php $PHP_SELF?>" method="post">

<select name="location">

<option value="http://www.google.com"> Google </option>
<option value="http://www.iii.co.uk"> Interactive Investor </option>
<option value="http://www.myspace.com"> Myspace </option>
<option value="http://www.facebook.com"> Facebook </option>

</select>
<input type="submit" name="submit" value="GO">

</form>
</body>
</html>
The page displays all of the options available in a dropdown list but when I click on the go button it does not take me to the appropriate page. Global Registers are turned off if that makes any difference!!
Any ideas???

Thanks Lee
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

the following line should look like so:

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
However it isn't good practice to use phpself. You can use '#' in place of it. That will redirect php to the current page.

Wayne
leegee3
Forum Newbie
Posts: 10
Joined: Thu Mar 29, 2007 7:28 am

Post by leegee3 »

Hi, I just tried your suggestion but it dodnt work.

After clicking on the go button, it just redirects itself to the same page with the dropdown list, which is the same as was happening previously.

Any other suggestions. I wont be able to sleep unless I get this sorted, ha.

Lee
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

check this out and see if this will work for you:

Code: Select all

<?php

if (isset($_POST['submitted'])){
	$location = $_POST['location'];
		switch ($location)
                        {
			case "http://www.google.com":
				//code for google.com
				break;

			case "http://www.facebook.com":
				//code for facebook.com;
				break;

			case "http://www.myspace.com":
				//code for myspace.com;
				break;

			case "http://www.iii.co.uk":
				//code for iii.co.uk;
				break;
                       {
}else{
?>

<html><head><title> Redirect </title></head>
<body>

Please select a site:

<form action="#" method="post">

<select name="location">

<option value="http://www.google.com"> Google </option>
<option value="http://www.iii.co.uk"> Interactive Investor </option>
<option value="http://www.myspace.com"> Myspace </option>
<option value="http://www.facebook.com"> Facebook </option>

</select>
<input type="submit" name="submit" value="GO">
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
}
?>
</body>
</html>
Something like that
leegee3
Forum Newbie
Posts: 10
Joined: Thu Mar 29, 2007 7:28 am

Post by leegee3 »

Hey, that PHP code didnt work either. The page redirects, but it goes to a blank page now, and in the url, it places a # at the end of it.
big_mumma
Forum Newbie
Posts: 4
Joined: Sat Apr 28, 2007 9:12 pm

Post by big_mumma »

fyi: I pasted the code you initially provided into a php page, and uploaded it to my server, tested it, and works fine.

Don't think its a coding issue as it worked fine for me...
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Re: Redirect options list not working

Post by Grim... »

leegee3 wrote:Global Registers are turned off if that makes any difference!!
You need to add a line to your script

Code: Select all

<?php 
$location = $_POST['location']; // new line
if($location) //an easier way to see if a variable has any content
{ 
        header("Location:$location"); 
        exit(); 
} 

?>
Post Reply