How can I add GET variables to the end of the current page url using a form with php?
I have some database information that is being shown on a page.
I am using a pagination class that uses the $_GET['page']
variable in the url. When you click on a different pagination anchor tag, it changes $_GET['page']
to a new number in the url and shows the corresponding results.
I have sort and search features which uses the $_GET['searchby']
and $_GET['search_input']
variables. The user enters their search criteria on a form that is using GET. The variables are then put into the url allowing for the correct results to be shown.
The problem I am having is that whenever I click on a pagination link, it开发者_运维技巧 adds the $_GET['page']
variable to end of the url and erases $_GET['searchby']
or $_GET['search_input']
. When I submit the search form, it adds $_GET['searchby']
and $_GET['search_input']
but erases $_GET['page']
.
How can I add GET variables to the end of the current page url using the anchor tag and search/sort form without having it erase any existing GET variables, but overriding them if they're the same GET variable name?
Try this:
if (strpos($_SERVER['REQUEST_URI'], '?') !== false)
{
$url = $_SERVER['REQUEST_URI'] . '&var=value';
}
else
{
$url = $_SERVER['REQUEST_URI'] . '?var=value';
}
<a href="<php echo $url;>">Go</a>
Note that $_SERVER['REQUEST_URI']
gives you current url including query string value.
$query_string = http_build_query(array_merge($_GET, array('page' => $page)));
精彩评论