Reputation: 11464
I'm trying to add paging functionality to a search items page. so I have added a pager with UL
as
echo '<ul>';
for($i=1; $i<=$pageCount; $i++)
{
echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?page=' . $i . '">' . $i . '</a>';
}
echo '</ul>';
When I click on a Page No in Pager, I can get the page number clicked on by
if (isset($_GET['page']))
{
$pageNo = $_GET['page'];
}
but I can not retain the text entered by the user to search items. I tried $_POST['txtSearchText']
but it does not retain the value after the page refreshed.
Is there a way to retain the from values (without using session) after self loading the page by hyperlink click?
Upvotes: 2
Views: 845
Reputation: 24182
You will either have your links submit a hidden form with your search parameters, or you serialize them as parameters to your query string to send along with the page number.
In the first case you need Javascript (so your "A" link will in fact set a hidden field in the form with the appropriate page number to go to, and submit the form). In the second case you do not need it, but you make the query string less human-friendly.
Otherwise there is session (you can save your search in a session object and maybe use a token in your paging links, in order to allow several pages open at once) or even cookies.
Upvotes: 6
Reputation: 83
For GET-based search paging, what I do is create a class called "Params", which includes the following methods, amongst others: readURLParams and generateURLParams. It also contains a member variable for each potential parameter, such as search terms, location, page number, filters, etc. When a page is loaded, you first run "readURLParams" which will read all the GET parameters into the object's member variables. When you are ready to create links for the paging functionality, you then run genereateURLParams, which will create the URL string that you will append to the paging link.
Now you no longer have to manually keep track of all the URL paramters, and you can add additional functionality to the class if you want to change your method to POST or some other form of parameter passing.
Upvotes: 0
Reputation: 4346
If you are using GET requests, you can just add the search query to the link:
echo '<ul>';
for($i=1; $i<=$pageCount; $i++)
{
echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?page=' . $i . '&query='.url_encode($search_query).'">' . $i . '</a>';
}
echo '</ul>';
If you wish to use POST requests, you should submit the form like @Palantir said or use AJAX.
I wouldn't use session for storing search queries due to possible collisions when two different queries are opened in two browser tabs.
Upvotes: 2