Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php get the post data when using pagination script

I have a page called mainPage.php that contain a pagination script which displays a list of pages.

 [1][2][3][4][5] 

the pagination works fine but when I go to the next page the post data disappear.

for($i=1;$i<=$totalPage;$i++)
{
if($page==$i)
{
echo ' <input  type="button" value="'.$i.'"> ';
}
else
{
echo '<a href="mainPage.php='.$i.'"> <input type="button" value="'.$i.'"> </a>';
}}

is there any way to get the post data to the next page number after clicking this link:

 <a href="mainPage.php='.$i.'"> 

without the form.

like image 761
user2521365 Avatar asked Jun 08 '26 23:06

user2521365


1 Answers

No, it's not possible. When a form is submitted (with method=post) to the server, that's one POST request. If you want to make another POST request, you need to make another POST request. If you click a link, that's not a POST request.

I'm assuming your scenario is something like a search form, which is submitted via POST and which returns several pages of results. In this case, POST is misused anyway. An HTTP POST request should be used for altering data on the server, like registering a new user or deleting a record. Just a search form is not data alteration, it's just data retrieval. As such, your form should be method=get. That will result in a URL with the form values as query parameters:

mainPage.php?search=foobar

You can then trivially create pagination URLs from that:

printf('<a href="mainPage.php?%s">...', http_build_query(array('page' => $i) + $ _GET));

Which will result in:

mainPage.php?search=foobar&page=2

This way all your requests are self contained GET queries.

like image 180
deceze Avatar answered Jun 10 '26 14:06

deceze