salmon
salmon

Reputation: 41

Redirect user upon form submission

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.

Upvotes: 0

Views: 1004

Answers (7)

Core Xii
Core Xii

Reputation: 6441

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);

Upvotes: 0

halfdan
halfdan

Reputation: 34234

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();

Upvotes: 1

gianebao
gianebao

Reputation: 18948

        <?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>

Upvotes: 0

Extrakun
Extrakun

Reputation: 19325

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.

Upvotes: 0

Zilverdistel
Zilverdistel

Reputation: 1579

<?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>

Upvotes: 0

Ruel
Ruel

Reputation: 15780

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)

Upvotes: 0

Sjoerd
Sjoerd

Reputation: 75649

Redirect using a location header:

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

Upvotes: 0

Related Questions