Storing $_SESSION variables

I’ve got a problem. I’m sure I’m being really stupid but I can’t seem to be able to rertive a $_SESSION variable.

I run through the code three times using a variable called $setup which I post each time as reset. The first time I go through the code, $setup has no value. The second time, it has the value of 1 and a SESSION variable called value 1 gets set from a POST. The third time, setup has the value of 2. I set a SESSION variable to the value of another POST but the orignal SESSION variable seems to have disappear according to using print_r to find out what SESSIONS are set.

 <?php
  session_start();
  $setup=$_POST['reset'];
  if ($setup==NULL)
  {
    $setup=0;
  }
  elseif ($setup==1)
  {
    $_SESSION['value1']=$_POST['value1'];
    $value1=$_SESSION['value1'];
  }
  elseif ($setup==2)
  {
    $value1=$_SESSION['value1'];
    $_SESSION['value2']=$_POST['value2'];
    $value2=$_SESSION['value2'];
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
  <head>
    <title>
      Program
    </title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
    <meta name="author" content="Richard Derek Young"/>
  </head>
 
  <body>
    <h1>
      Program
    </h1>
    <p>
      Use this program to create it.
    </p>
<?php
    if ($setup==0)
    {
?>
      <p>
        <form method="post" name="world" action="program.php">
          Enter value 1. <br/>
          <input type="text" name="value1"/>
<?php
          $setup++;
?>
          <input type="hidden" name="reset" value="<?=$setup;?>"/>
          &nbsp; <input type="submit" value="Next"/>
        </form>
      </p>
<?php
    }
    elseif ($setup==1)
    {
      $setup++;
?>
      <form method="post" name="description" action="program.php">
        <input type="hidden" name="reset" value="<?=$setup;?>"/>
        <p>
          <input type="radio" name="value2" value="1" onclick="javascript:document.description.submit()"/> &nbsp; Option 1 <?=$value1;?>.
        </p>
        <p>0
          <input type="radio" name="value2" value="2" onclick="javascript:document.description.submit()"/> &nbsp; Option 2 <?=$value1;?>
        </p>
        <p>
          <input type="radio" name="value2" value="3" onclick="javascript:document.description.submit()"/> &nbsp; Option 3 <?=$value1;?>.
        </p>
        <p>
          <input type="radio" name="value2" value="4" onclick="javascript:document.description.submit()"/> &nbsp; Option 4 <?=$value1;?>.
        </p>
        <p>
          <input type="radio" name="value2" value="5" onclick="javascript:document.description.submit()"/> &nbsp; Option 5 <?=$value1;?>.
        </p>
        <p>
          <input type="radio" name="value2" value="6" onclick="javascript:document.description.submit()"/> &nbsp; Option 6 <?=$value1;?>.
        </p>
        <p>
          <input type="radio" name="value2" value="7" onclick="javascript:document.description.submit()"/> &nbsp; Option 7 <?=$value1;?>.
        </p>
      </form>
<?php
    }
    elseif ($setup==2)
    {
      $setup++;
      echo $value2 . " " . $value1 . ".";
?>
      <p>
        More Stuff.
      </p>
<?php
    }
?>
    <p>
      <button onclick="<? session_destroy();?>window.location.href = 'program.php';">Reset</button>
    </p>
  </body>
</html>

It’s on line 90 that I only get value2 echoed.

I think changing the following lines will make your script work.

[php][/php]
to
[php][/php]

and similarly change following
[php]<?=$value1;?>[/php]
to
[php]<?php echo $value1;?>[/php]

You need to use echo, whenever you want to display something as HTML and don’t use shortcuts, stick with the standards.

Will that make a difference. I’ve never had that problem before. The problem isn’t with printing, it’s the SESSIONS not being carried over.

I think so.

Oh sorry. It’s a shortcut to echo, I think something else is wrong with your code. I will let you konw, If I can solve the issue.

Umm, every page load you are telling php to session_destroy();
[php]


Reset

[/php]
Not really sure what you are trying to do here anyway since that command has no html value and shouldn’t be placed in a instance like that.

Thanks. You’re right. When working with SESSIONS it often need to reset it and so created a button to destroy the SESSION but of course PHP is server side.

Thanks. You’re a star.

Sponsor our Newsletter | Privacy Policy | Terms of Service