Page 1 of 1

goto URL

Posted: Thu Nov 06, 2008 1:36 pm
by soundjam
Good afternoon,
I am new to the PHP lang all together.
I am wondering if anyone can help me out?

What I am trying to do is, on my index page have users answer the question of what their location is.
Then, pass this information off to location.php where it will direct the user to one of two web pages.
Any Ideas? and Thank you in advance,
Brian

index.html
<html>
<head></head>
<body>
<h2>Please Select Your Location</h2>
<p>
<form method="get" action="location.php">
<select name="location">
<option value="1">Ontario - Canada
<option value="2">Other Provinces - Canada
</select>
<input type="submit" value="Send">
</form>
</body>
</html>


location.php code

<?php
// get form selection
$location = $_GET['location'];
// check value and select appropriate item
if ($location == 1) {
$special = 'Location: http://www.url_A.com';
}
elseif ($location == 2) {
$special = 'Location: http://www.url_B.com';
}
?>
<?php header( $special); ?>

Re: goto URL

Posted: Thu Nov 06, 2008 3:11 pm
by sparrrow
Well the code you posted looks like it will work in theory. Are you getting an error or unexpected result?

Re: goto URL

Posted: Thu Nov 06, 2008 3:16 pm
by soundjam
this is the error that comes up:
Warning: Cannot modify header information - headers already sent by (output started at /home/bria4964/public_html/location.php:3) in /home/bria4964/public_html/location.php on line 17

I cannot get it to forward.
Brian

Re: goto URL

Posted: Thu Nov 06, 2008 3:36 pm
by soundjam
I just remembered hearing that spaces or empty line feeds above the <?php line should not be there..
I have removed them and it's working, Thank you for your reply.

Re: goto URL

Posted: Thu Nov 06, 2008 3:44 pm
by sparrrow
You can clear out any printed data before the header data is executed like you say you did and that works. Some times this is impractical though, and you can put this on the first line of the page:

Code: Select all

 
<?php
ob_start();
?>
 
This will allow headers to be modified after the page is rendered.