Forum Moderators: coopster

Message Too Old, No Replies

Redirecting page in PHP

Help with redirect with if statement.

         

mespejo

11:07 pm on Jan 8, 2007 (gmt 0)

10+ Year Member



I have created a form that allows user to select a radio button and based on the button they select when they click on Submit it will redirect them to another page.

How do I do code it in PHP?

eelixduppy

11:14 pm on Jan 8, 2007 (gmt 0)



You can redirect with php by setting the location header:

[url=http://us3.php.net/header]header[/url]("Location: location.html");
exit();

Of course location.html will be replaced with dynamic content from your field using the $_POST super global.

Is this within a form that has other fields or are you using it for navigation?

mespejo

1:25 am on Jan 9, 2007 (gmt 0)

10+ Year Member



Yes it is within the form that has other fields. Can you please write a code on how to create an if statement with the header() function? And also how to set values in the form.

Thanks.

coastie

4:48 pm on Jan 9, 2007 (gmt 0)

10+ Year Member



This is only a suggestion, but rather than doing an explicit conditional redirect in php, you might well consider applying a rewrite rule to your .htaccess file (assuming you're using Apache w/mod_rewrite).

However, if you have a form similar to this:

<form action="http://yourplace.com/form.php" method="post">
<input type="radio" name="form-01" value="pick-1"/> pick-1<br/>
<input type="radio" name="form-01" value="pick-2"/> pick-2<br/>
<input type="radio" name="form-01" value="pick-3"/> pick-3<br/>
<input type="submit" value="submit"/>
</form>

Then form.php might go something like this:

<?php

$selection = $_POST['form-01'];

if(!(strcmp($selection, 'pick-1'))) writepage1();
if(!(strcmp($selection, 'pick-2'))) writepage2();
if(!(strcmp($selection, 'pick-3'))) writepage3();

function writepage1()
{
echo "<html><body> this is page-1</body></html>";
}

function writepage2()
{
echo "<html><body> this is page-2</body></html>";
}

function writepage3()
{
echo "<html><body> this is page-3</body></html>";
}
?>

If you still wish to redirect from these pages, you can add a location header to the generated html or (more elegantly) use eelixduppy's suggestion and call header() in place of writepage1() etc.

ta