ScrewLooseSalad Posted March 8, 2013 Share Posted March 8, 2013 I'm trying to $_POST a variable or two back to the same page, so that I can scroll through different years of entries in my database... I'm not having any luck with it so far, is anyone able to tell me where I've gone wrong? I've tried a few different techniques, but my trouble boils down to being unable to post to the same page,initially I tried <a href="">'s with the variables on the end, but following documentation online its become a more complicated mess with the same effect... adding in $_SERVER['PHP_SELF'], a <form> and such... <?php $up=$_POST['up']; $dn=$_POST['down']; $year = date("Y"); if(up){$year++;} if(dn){$year--;} ?> <h2 style="font-family: ErasDemi;">Log for <?php echo $year; ?></h2> <form action=<?php echo $_SERVER['PHP_SELF'] ?> method='post'> <input type='submit' name='dn' value='Last Year'/> <input type='submit' name='up' value='Next Year'/> </form> Any help would be much appreciated. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 8, 2013 Share Posted March 8, 2013 (edited) if(up){$year++;} if(dn){$year--;} 'up' and 'dn' are not valid variables. Also, if your intent is to let the user continuously increment/decrement the $year then you need to pass something other than whether the user select up or down. I suspect you start $year as the current year and want to allow the user to continue to increment and decrement. So, you should instead pass a value of how many years to increment or decrement from the default. This is very easy to do with $_GET or $_POST Here is an example page on how you can do this using GET or POST <?php error_reporting(E_ALL); session_start(); $current_year = date('Y'); //Determine year if using POST method $postOffset = isset($_POST['post_offset']) ? intval($_POST['post_offset']) : 0; if(isset($_POST['dn'])) { $postOffset--; } if(isset($_POST['up'])) { $postOffset++; } $postYear = $current_year + $postOffset; //Determine year if using GET method $getOffset = isset($_GET['get_offset']) ? intval($_GET['get_offset']) : 0; $getYear = $current_year + $getOffset; $getLast = $getOffset - 1; $getNext = $getOffset + 1; ?> <html> <head></head> <body> POST Year: <?php echo $postYear; ?><br><br> <form action="" method="post"> Change the year using $_POST<br> <input type="hidden" name="post_offset" value="<?php echo $postOffset; ?>" /> <input type='submit' name='dn' value='Last Year'/> <input type='submit' name='up' value='Next Year'/> </form> <br><br><br><br> Get Year: <?php echo $getYear; ?><br><br> Change the year using $_GET<br> <a href="?get_offset=<?php echo $getLast; ?>">Last Year</a> <a href="?get_offset=<?php echo $getNext; ?>">Next Year</a> </form> </body> </html> Edited March 8, 2013 by Psycho 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.