Jump to content

[SOLVED] $_SESSION variable not updating


soycharliente

Recommended Posts

I've been agonizing over this for the past 4 hours. I'm building a calendar that has buttons to change what month/year is displayed. Hopefully I'll include all relevant information, so here we go.

 

I'm using session variables to store data. Here's the top of the index.php file:

<?php
session_start();
session_register("CurrentMonth"); //to always know what this month is
session_register("CurrentDay"); //to always know what this day is
session_register("CurrentYear"); //to always know what this year is
session_register("MonthForCalendar"); //what month to show on the calendar
session_register("YearForCalendar"); //what year to pull the month from for the calendar
?>

 

Here is the code to try and update the session variables:

<?php
isset($_POST["prevMonth"]) ? $_SESSION["MonthForCalendar"] = getPrevMonth($_SESSION["MonthForCalendar"]) : $_SESSION["MonthForCalendar"] = date("n");
isset($_POST["nextMonth"]) ? $_SESSION["MonthForCalendar"] = getNextMonth($_SESSION["MonthForCalendar"]) : $_SESSION["MonthForCalendar"] = date("n");
isset($_POST["prevYear"])  ? $_SESSION["YearForCalendar"]  = getPrevYear($_SESSION["YearForCalendar"])   : $_SESSION["YearForCalendar"]  = date("Y");
isset($_POST["nextYear"])  ? $_SESSION["YearForCalendar"]  = getNextYear($_SESSION["YearForCalendar"])   : $_SESSION["YearForCalendar"]  = date("Y");
?>

 

Here is the form:

<form action="index.php" method="post">
<input class="calendarSubmit" type="submit" name="prevMonth" value="«" />
<?php echo getTextMonth($_SESSION["MonthForCalendar"]); ?>
<input class="calendarSubmit" type="submit" name="nextMonth" value="»" />
 
<input class="calendarSubmit" type="submit" name="prevYear" value="«" />
<?php echo $_SESSION["YearForCalendar"]; ?>
<input class="calendarSubmit" type="submit" name="nextYear" value="»" />
</form>

 

Here are some functions I may have mentioned thus far:

<?php
function getPrevMonth($m) { return $m == 1 ? 12 : $m--; }
function getPrevYear($y) { return $y--; }
function getNextMonth($m) { return $m == 12 ? 1 : $m++; }
function getNextYear($y) { return $y++; }
function getTextMonth($m) { return date("F", mktime(0,0,0,$m,1,1985)); }
?>

 

I have printed out both the post and session arrays and looked at their values while trying to use the calendar. The buttons are registering being clicked and being set to a value. The variables for what month/year to use on the calendar do not change however.

 

I know this post is rather long, but any help is GREATLY appreciated. Thanks in advance because my eyes are starting shake.

 

Page can be seen at www.charlieholder.com/areas/wbc.

Link to comment
https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/
Share on other sites

Session_Register is where you are faulting at.

 

www.php.net/session_register

 

read the caution, it is said it has been depreciated and will not work if register_globals is off. Best not to use it. Instead use $_SESSION

 

<?php
session_start();
$_SESSION["CurrentMonth"]; //to always know what this month is
$_SESSION["CurrentDay"]; //to always know what this day is
$_SESSION["CurrentYear"]; //to always know what this year is
$_SESSION["MonthForCalendar"]; //what month to show on the calendar
$_SESSION["YearForCalendar"]; //what year to pull the month from for the calendar
?>

 

Try that out.

<?php
session_start();

if (!isset($_SESSION['CurrentMonth'])) {
$_SESSION["CurrentMonth"]; //to always know what this month is
$_SESSION["CurrentDay"]; //to always know what this day is
$_SESSION["CurrentYear"]; //to always know what this year is
$_SESSION["MonthForCalendar"]; //what month to show on the calendar
$_SESSION["YearForCalendar"]; //what year to pull the month from for the calendar
}
?>

 

Something like that.

I am convinced that there is something wrong with these lines (updates session variables when button is clicked):

<?php
isset($_POST["prevMonth"]) ? $_SESSION["MonthForCalendar"] = getPrevMonth($_SESSION["MonthForCalendar"]) : "";
isset($_POST["nextMonth"]) ? $_SESSION["MonthForCalendar"] = getNextMonth($_SESSION["MonthForCalendar"]) : "";
isset($_POST["prevYear"]) ? $_SESSION["YearForCalendar"]  = getPrevYear($_SESSION["YearForCalendar"]) : "";
isset($_POST["nextYear"]) ? $_SESSION["YearForCalendar"]  = getNextYear($_SESSION["YearForCalendar"]) : "";
?>

 

Page can be seen at http://www.charlieholder.com/areas/wbc/.

The 'MonthForCalendar' and 'YearForCalendar' session variables are only getting created once so I fixed the problem of them being reset every time the page loads. If you go to the link, you'll see text only the first time the page loads that the session variables are included on the page.

 

The buttons are registering as being clicked, and when I test whether or not they are set to something, it evaluates TRUE. I don't know what's wrong. I has to be a problem with logic.

 

Code that outputs when the button is clicked:

<?php
echo isset($_POST["prevMonth"]) ? "prevMonth is set to something" : "";
echo isset($_POST["nextMonth"]) ? "nextMonth is set to something" : "";
echo isset($_POST["prevYear"])  ? "prevYear is set to something" : "";
echo isset($_POST["nextYear"])  ? "nextYear is set to something" : "";
?>

Ok. Solved the problem. Thanks to all that tried to help.

 

The problem was using $var--;

I'm pretty sure it returns the value THEN decrements. So everything was working exactly how it should, I just wrote incorrect code. LOL

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.