Jump to content

Recommended Posts

Hello, new to php. having trouble storing one particular variable into the session.

Here is where I want to echo the result once the user has returned to the page:

<?php
if ($_POST['submit']) {
$show = $result;
$_SESSION['show'] = $show;

else {
$show = $_SESSION['show'];
echo "The last result was: ". $show;
}
?>

 

//inbetween here a form is created

Here is where the $result variable is created:

<?php 
$decision = $_POST['option'][array_rand($_POST ['option'])];
if (!$decision){
$result = $_POST['default'];
echo $result;
}

else{
$result = $decision;
echo $result;
}
?>




Now the strange thing is if I change "$show = $result;"
to "$show = $_POST['default']; 
That does store and displays when the user returns t the page.

 

So everything else works apart from "$result" being stored. 

Any help would be great! spent hours going mad on this!

 

Link to comment
https://forums.phpfreaks.com/topic/276941-variable-wont-store-in-session/
Share on other sites

Hi yeah I have :

 

 

<?php 
session_name('Session1');
session_set_cookie_params(6000);
session_start(); 
 
?>
 
All at the start of my code, still the "$result" variable does not store or display, however "$_POST['default']" does, any idea what the issue might be?
Thanks 
 
*Edit "$result" variable
Edited by thecactusjack

As the function name implies, you need session_start() before you use session variables

 

Yes I do have this at the very start.

Like I said if I store the "$_POST['default']" variable as opposed to the "$result" variable in the session, it stores and echos once the user returns to the page as I intended.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.