thecactusjack Posted April 14, 2013 Share Posted April 14, 2013 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:<?phpif ($_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! Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 14, 2013 Share Posted April 14, 2013 session_start() must be at least in the file. preferably the first line of each file. Quote Link to comment Share on other sites More sharing options...
thecactusjack Posted April 14, 2013 Author Share Posted April 14, 2013 (edited) 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 April 14, 2013 by thecactusjack Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 14, 2013 Share Posted April 14, 2013 Honestly your code and description doesn't make any sense. Start debugging by echoing every variable when you need to use it and see what it is. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 14, 2013 Share Posted April 14, 2013 As the function name implies, you need session_start() before you use session variables Quote Link to comment Share on other sites More sharing options...
thecactusjack Posted April 14, 2013 Author Share Posted April 14, 2013 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 14, 2013 Share Posted April 14, 2013 Don't PM members asking for help, that's what the forum is for. Obviously $result isn't set so figure out why. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.