views:

38

answers:

3

I have a multi-step form, let's say for the sake of ease it's 2 steps. First step I want to select a radio button and based on that radio button selection it takes me to a certain page, but I also want that selection stored in a session. I have 2 pages: page1.php

session_start();

if(isset($_POST['post'])) {
if (($_POST['country'] == 'US')) {
header("Location: US_Products.php"); }
elseif (($_POST['country'] == 'CDN')) {
header("Location: CDN_Products.php"); }
else { die("Error"); }
exit;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="USA">USA:</label>
<input type="radio" name="country" value="US">
<label for="CDN">Canada:</label>
<input type="radio" name="country" value="CDN">
<input type="submit" name="post" value="Go To Filter">
</form>

Page2.php (either A or B)

session_start();
$_SESSION['country'] = $_POST['country'];
<?php echo $_SESSION['country']; ?>

The Country choice is not being passed when I have it do this conditional redirect. Is there a problem with session variables and redirects or session variables and PHP_SELF or something?

A: 

Set the session variable before you redirect - the post is lost, as the redirect is essentially a regular GET request

if (($_POST['country'] == 'US')) 
{
  $_SESSION['country'] = $_POST['country'];
  header("Location: US_Products.php"); 
}
elseif (($_POST['country'] == 'CDN')) 
{
  $_SESSION['country'] = $_POST['country'];
  header("Location: CDN_Products.php"); 
}
else 
{ 
  die("Error"); 
}
Paul Dixon
That didn't work
Rob Bennet
A: 

You're trying to store the value ($_POST['country']) after the redirect happens. You should store it in $_SESSION before calling 'header', then you will be able to retrieve it in Page2.php.

Also, you might be better off using 'include' instead of redirects.

bazmegakapa
What do you mean using "include". Could you give an example?
Rob Bennet
Already shown in an other answer.
bazmegakapa
+2  A: 

Page 1:

session_start();

if(isset($_POST['post'])) {
    $_SESSION['country'] = $_POST['country'];
    if (($_POST['country'] == 'US')) {
        header("Location: US_Products.php"); }
    elseif (($_POST['country'] == 'CDN')) {
        header("Location: CDN_Products.php"); }
    else { die("Error"); }
    exit;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="USA">USA:</label>
<input type="radio" name="country" value="US">
<label for="CDN">Canada:</label>
<input type="radio" name="country" value="CDN">
<input type="submit" name="post" value="Go To Filter">
</form>

Page 2:

session_start();
<?php echo $_SESSION['country']; ?>

Or using include method, just use one page:

session_start();

if(isset($_POST['post'])) {
    if (($_POST['country'] == 'US')) {
        include("US_Products.php"); }
    elseif (($_POST['country'] == 'CDN')) {
        include("CDN_Products.php"); }
    else { die("Error"); }
    exit;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="USA">USA:</label>
<input type="radio" name="country" value="US">
<label for="CDN">Canada:</label>
<input type="radio" name="country" value="CDN">
<input type="submit" name="post" value="Go To Filter">
</form>

and you should be able to use echo $_POST['country'] on US_Products.php and CDN_Products.php, or

bemace
YOU ROCK!!! So defining it on the second page was screwing it up? This might be a stupid question, but do you always define session variables to POST variables on the page where the POST is input?
Rob Bennet
once you call `header`, you've lost your `$_GET` and `$_POST` data because you've gone to another page. So your options are to store it in a session variable before calling header, or pass the data through the header call by doing something like `header("Location: US_Products.php?country=$_POST[country]");`, which would make `country` available in the `$_GET` array of US_Products.php
bemace