Forum Moderators: coopster

Message Too Old, No Replies

problem with redirection

unable to redirect using header()

         

sunnydayzrback

4:54 pm on May 19, 2007 (gmt 0)

10+ Year Member



hi,
I wrote this code to make the user select a site from a dropdown menu and then redirect to the site he selected. But its not getting redirected

?php
if($location!=null)
{
header(' Location: $location' );
exit();
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>redirect</title></head>
<body>
choose a site to visit:
<form action="<?php echo ($PHP_SELF);?>" method ="post">
<select name ="loc"><?php $location=$_POST["loc"];?>
<option value="http://www.google.com">Google</option>
<option value="http://www.yahoo.com">Yahoo</option>
<option value="http://www.gmail.com">Gmail</option></select>
<input type="submit" name="submit" value="Go">
</form>
</body>
</html>

i select a site and then submit the form but nothing happens. what may be the problem?

Thanks in advance

dreamcatcher

5:00 pm on May 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there

You are accessing a var that doesn`t exist when you post the form. Try:

if($_POST['loc'])
{
header('Location: '.$_POST['loc']);
exit();
}

dc

sunnydayzrback

5:34 pm on May 19, 2007 (gmt 0)

10+ Year Member



I tried this, but i have the same problem as before

<?php
if($_POST['loc']!= null)
{
header('Location: $_post["loc"]');
exit();
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>redirect</title></head>
<body>
choose a site to visit:
<form action="<?php echo ($PHP_SELF);?>" method ="post">
<select name ="loc"><?php $location=$_POST["loc"];?>
<option value="http://www.google.com">Google</option>
<option value="http://www.yahoo.com">Yahoo</option>
<option value="http://www.gmail.com">Gmail</option></select>
<input type="submit" name="submit" value="Go"><? php echo("$location");?>
</form>
</body>
</html>

dreamcatcher

6:04 pm on May 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try changing from single quotes to double quotes:

if(isset($_POST['loc']))
{
header("Location: ".$_POST['loc']."");
exit();
}

dc

sunnydayzrback

1:30 pm on May 20, 2007 (gmt 0)

10+ Year Member



Worked . Thanks very much.

IndiaMaster

9:27 am on May 22, 2007 (gmt 0)

10+ Year Member



Hi,

I am using the following function for redirection. Just provide the URL and it works.

function gotopage($url)
{
echo "<script language=\"javascript\">";
echo "window.location = '".$url."'; \n";
echo "</script>";
}

dreamcatcher

10:00 am on May 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks IndiaMaster. Worth pointing out though that if javascript is disabled, that code won`t work.

dc

delboy1978uk

12:22 pm on May 22, 2007 (gmt 0)

10+ Year Member



good point about the javascript being disbled.
Question.
I have just used javascript to redirect.

Firstly it opens a page with a form for printing.
obviously as it is a printed form, it has no links etc
so i have opened it in a new window with the window.open javascript function.

Next it opens the summary page in the original window using window.location

however - How could i do this in php only?
i can get it to change page using the php header command
but what about the other popup page?