tags:

views:

53

answers:

7

Hey i want to use php to redirect a user to a url depending on which radio buttons they have been selected in the form once they click the submit button.

I have this thus far but it does not work so its pretty useless.

        <?php // variables form form
        $Country = $_POST['Country'];
        ?>

        <form action="<?php print $Country ?>" method="post">
        <input name="Country" type="radio" value="http://www.istockphoto.com" /> South Africa<br />
        <input name="Country" type="radio" value="http://www.jamieburger.co.za" /> England<br />
        <input name="" type="submit" value="Submit"/>
        </form>

Any help would be greatly appreciated.

A: 

Redirect using a location header:

if ($Country == 'England') {
    header('Location: http://www.jamieburger.co.za');
}
Sjoerd
+1  A: 

If you want to redirect "on submit" you need to use JavaScript to change the submit URL depending on your "radio" state. I suggest you let the server decide to which URL the user gets redirected.

Just do the following on the server-side:

header("Location: ".$Country);
exit();

BUT (!!) check if the URL is valid, otherwise you will redirect to any URL the user submits. It would be better style to have the country name as "value" for your radio button too. E.g.

<input type="radio" name="Country" value="Germany">Germany</input>

You could then use the following code to redirect:

$country = $_POST['Country'];
switch($country) {
  case 'Germany':
      $url = "http://country/germany";
  case 'England':
      $url = "http://country/england";
  default:
      $url = "http://country/invalid";
}

header("Location: ". $url);
exit();
halfdan
A: 

use header() Example:

<?php
if (isset($_POST['Country'])) header('Location: $_POST['Country']');
?>

http://php.net/manual/en/function.header.php

Remember to put this code at the very top of the page (before any HTML tag)

Ruel
A: 
<?php 
    $country = $_POST['Country'];
    if (!empty($country))
      header( 'Location: '.$country ) ;
?>

<form action="<?php print $_SERVER['PHP_SELF']; ?>" method="post">
  <input name="Country" type="radio" value="http://www.istockphoto.com" /> South Africa<br />
  <input name="Country" type="radio" value="http://www.jamieburger.co.za" /> England<br />
  <input name="" type="submit" value="Submit"/>
</form>
zilverdistel
Thanks everyone for your comments. Works great now i just need to get it to work in my facebook application.
salmon
A: 

First thing, the action parameter of form should be a url, either absolute or relative, though usually relative.

<form action='process.php?action=country'>

// submit to the same calling page
<form action='?action=country'>

You are using the wrong mechanic to redirect. The action of the form is where the data is sent to after the form is submitted. Since you are submitting to the same page, you can just use '?' as your action, or $_SERVER['PHP_SELF']. To redirect to another page base on a condition, use header(), as pointed out by others.

<?php

$country = $_POST['Country'];

// redirect    
 header("Location: $Country");
exit();
?>

Note: it's better to code the URLs in an array, and use the index of the radio button to fetch the right URL to redirect to. It also makes it easier to read in the URls to redirect to from a DB.

Extrakun
A: 
        <?php // variables form form
        if (!empty($_POST['Country']) {
            header('location: ' . $_POST['Country']);
        }
        ?>

        <form action="" method="post">
        <input name="Country" type="radio" value="http://www.istockphoto.com" /> South Africa<br />
        <input name="Country" type="radio" value="http://www.jamieburger.co.za" /> England<br />
        <input name="" type="submit" value="Submit"/>
        </form>
sheeks06
A: 

The 303 redirect was designed specifically for this. Simply header('Location: http://'); actually sends a 302, which is sort of deprecated in HTTP/1.1. Here's what I do:

header($_SERVER['SERVER_PROTOCOL'] . ' 303 See Other', true, 303);
header('Location: ' . $url);
Core Xii