drumichael Posted November 15, 2012 Share Posted November 15, 2012 Thanks for anyone's help in advance! I am hoping to have a php variable updated with the click of a link. I currently have at the top of my web app something like this: < November 2012 > The arrows are previous and next, respectively. In the example above, "November 2012" comes from a php variable set to today's date by default upon page load. I would like for the arrow links to advance the php date variable by one month. Just to clarify: I realize there is probably an easier way to do this without using a php variable, but I really do need it to be saved in the variable because I also use that same date variable in other functions on my page. My current code looks something like this: <?php $qdate = time(); ?> <div class="month_nav"> <div class="previous"> <a href="#"><img src="/img/previous.png" alt="previous"/></a> </div> <div class="current"> <p><?php echo date('F Y', $qdate); ?></p> </div> <div class="next"> <a href="#"><img src="/img/next.png" alt="next"/></a> </div> </div> <div class="justclear"></div> In trying to figure this out, I thought about sending an HTML request with the link like this: <a href="/reports?next">Next</a> <?php $qdate = time(); if($_REQUEST['next']) { $qdate = $qdate + (30 * 24 * 60 * 60); } ?> The only problem with this so far is... it isn't working at all. LOL If anyone has any ideas on how to get this done, I'd greatly appreciate it!!! Quote Link to comment https://forums.phpfreaks.com/topic/270707-updating-a-php-variable-on-click/ Share on other sites More sharing options...
MDCode Posted November 15, 2012 Share Posted November 15, 2012 (edited) I would suggest looking into using an ajax/jquery request to call a php script that updates the time (makes it a lot easier). But either way, it should work. Then use something like the following: <?php $nextMonth = time() + (30 * 24 * 60 * 60); $twoMonths = time() + (60 * 24 * 60 * 60); $threeMonths = time() + (90 * 24 * 60 * 60); echo "In one month it will be $nextMonth"; echo "In two months it will be $twoMonths"; echo "In three months it will be $threeMonths"; // you get the idea hopefully. ?> I'm not sure by what you mean "isn't working at all" but if you mean it isn't updating at all, try changing REQUEST to GET Edited November 15, 2012 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/270707-updating-a-php-variable-on-click/#findComment-1392603 Share on other sites More sharing options...
AyKay47 Posted November 15, 2012 Share Posted November 15, 2012 <a href="/reports?next">Next</a> Unless you are using rewriteMod which I doubt, the above is an invalid URL and $_REQUEST['next'] will not contain anything. It needs to be something like href='reports.php?next=1' Quote Link to comment https://forums.phpfreaks.com/topic/270707-updating-a-php-variable-on-click/#findComment-1392676 Share on other sites More sharing options...
Zane Posted November 15, 2012 Share Posted November 15, 2012 (edited) <a href="/reports?next">Next</a> Unless you are using rewriteMod which I doubt, the above is an invalid URL If reports is a folder that contains an index.php file then the url is fine. All you have to do is check whether $_GET['next'] isset and then code around it. As far as changing a php variable on click, I dont believe that is possible. You would need to store the current month somewhere like in a $_SESSION variable... then use date("M", strotime("+1 month", $_SESSION['curMonth'])) to get the next month Edited November 15, 2012 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/270707-updating-a-php-variable-on-click/#findComment-1392682 Share on other sites More sharing options...
AyKay47 Posted November 15, 2012 Share Posted November 15, 2012 I did forget about that, my apologies. Quote Link to comment https://forums.phpfreaks.com/topic/270707-updating-a-php-variable-on-click/#findComment-1392696 Share on other sites More sharing options...
drumichael Posted November 16, 2012 Author Share Posted November 16, 2012 I'm actually doing this in wordpress, so yes I'm using rewriteMod. Not exactly sure why it would be so preposterous if I was, though. I didnt feel like it was neccessary for the request 'next' to contain anything since all I wanted to do was check to see if it was there or not and then execute my code. Thanks for your help, Zane. I'm not real experienced in using Session variables, so I am going to read up on that and give it a shot. I see your method in finding the next month, and I will just need to figure the next month then overwrite what it holds as the current month so that I can continuously click the next button to advance one month on each click. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/270707-updating-a-php-variable-on-click/#findComment-1392979 Share on other sites More sharing options...
nodirtyrockstar Posted November 16, 2012 Share Posted November 16, 2012 I recommend that your "next" field be a HTML <input /> tag with type "image." You will set the source, etc. It will need to be contained within a form tag and the action (should you decide to go with PHP) will just be to reload the script. You will need to add a check in the script to identify if that button was clicked, and thus trigger the update. You can do so by adding a name attribute to your input. The result will be something like this: <form action='reload_current_page.php' method='post'><input alt='Advance one month' type='image' src='filepath_to_your_image' name='next' title='Advance one month' /></form> As for the check in the script, you can write: if (!empty($_POST['next'])) { $_SESSION['today']['month'] = $_SESSION['today']['month']++ //(you will assign this differently depending on how you define your date variable. I used the increment operator to signify adding on one month) //This is where you would manipulate the variables if the key is pressed. } Of course, this does not cover any aspect of security, other than avoiding the pitfalls of using $_GET... Quote Link to comment https://forums.phpfreaks.com/topic/270707-updating-a-php-variable-on-click/#findComment-1393106 Share on other sites More sharing options...
nodirtyrockstar Posted November 16, 2012 Share Posted November 16, 2012 BTW, session variables allow you to persist memory on the server for the duration of the session. You can create new ones by assigning new variables, like I did above. It's pretty basic stuff. Quote Link to comment https://forums.phpfreaks.com/topic/270707-updating-a-php-variable-on-click/#findComment-1393108 Share on other sites More sharing options...
Christian F. Posted November 17, 2012 Share Posted November 17, 2012 Why do it the hard way? Make $timestamp a unix timestamp based upon the current shown month+year, and then use this: $nextMonth = date ("Ym", strtotime ("+1 month", $timestamp)); $nextLink = '<a href="reports?month='.$nextMonth.'">Next month</a>'; KISS. Quote Link to comment https://forums.phpfreaks.com/topic/270707-updating-a-php-variable-on-click/#findComment-1393148 Share on other sites More sharing options...
drumichael Posted November 19, 2012 Author Share Posted November 19, 2012 Christian, thanks so much. This is the way I'm going to do it!! I will check for a REQUEST, if a date is specified, I'll calculate the next and previous links from that. if not, i'll use today(). Nodirtyrockstar, thanks to you as well! I am going to start using SESSION now, too! Quote Link to comment https://forums.phpfreaks.com/topic/270707-updating-a-php-variable-on-click/#findComment-1393492 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.