Jump to content

posting back to the same page with $_POST


Recommended Posts

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


$year = date("Y");

<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'/>

Any help would be much appreciated.

Link to comment
Share on other sites




'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



$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;


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'/>

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>
Edited by Psycho
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.

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.