soycharliente Posted June 8, 2007 Share Posted June 8, 2007 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 More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 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. Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-270446 Share on other sites More sharing options...
soycharliente Posted June 8, 2007 Author Share Posted June 8, 2007 Nope. That did not work. I read some more cautions down the page and it said not to use session_start() in some cases. I tried taking that out too and it didn't do anything either. Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-270457 Share on other sites More sharing options...
trq Posted June 8, 2007 Share Posted June 8, 2007 Sorry, but these snippets of code give no indication of your scripts logic. Can we see your actual code? Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-270459 Share on other sites More sharing options...
soycharliente Posted June 8, 2007 Author Share Posted June 8, 2007 attached... It's really 4 files, but I added them all together for simplicity. [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-270465 Share on other sites More sharing options...
soycharliente Posted June 8, 2007 Author Share Posted June 8, 2007 @frost: So using $_SESSION instead of session_register() still works. THanks for the tip. Still can't get the session variables to update though. Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-270961 Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 You may need to do this come to think of it: $_SESSION["CurrentMonth"]=""; //to always know what this month is Set it equal to something for proper syntax. Just an fyi really. Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-270964 Share on other sites More sharing options...
soycharliente Posted June 8, 2007 Author Share Posted June 8, 2007 I actually did do that on my own after your advice the first time. Didn't work. Check the attachment a few posts down. Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-270968 Share on other sites More sharing options...
soycharliente Posted June 8, 2007 Author Share Posted June 8, 2007 *bump* Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-271121 Share on other sites More sharing options...
soycharliente Posted June 8, 2007 Author Share Posted June 8, 2007 OMG. I think I figured it out. But I don't know what to do to fix it. The problem is that the variables are being reset every time the page loads. What can I do so that it just initializes them once? Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-271144 Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 <?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. Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-271180 Share on other sites More sharing options...
soycharliente Posted June 9, 2007 Author Share Posted June 9, 2007 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" : ""; ?> Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-271349 Share on other sites More sharing options...
soycharliente Posted June 9, 2007 Author Share Posted June 9, 2007 *bump* Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-271409 Share on other sites More sharing options...
soycharliente Posted June 9, 2007 Author Share Posted June 9, 2007 *bump* Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-271487 Share on other sites More sharing options...
soycharliente Posted June 9, 2007 Author Share Posted June 9, 2007 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 Link to comment https://forums.phpfreaks.com/topic/54685-solved-_session-variable-not-updating/#findComment-271519 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.