Jump to content

Updating A Php Variable On Click


drumichael

Recommended Posts

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!!!

Link to comment
Share on other sites

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 by SocialCloud
Link to comment
Share on other sites

<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 by Zane
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.