如何使用带有 php 的表单将 GET 变量添加到当前页面 url 的末尾?

2022-01-04 00:00:00 forms get php pagination

我有一些显示在页面上的数据库信息.

I have some database information that is being shown on a page.

我使用的分页类在 url 中使用了 $_GET['page'] 变量.当您单击不同的分页锚标记时,它会将 $_GET['page'] 更改为 url 中的新数字并显示相应的结果.

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.

我有使用 $_GET['searchby']$_GET['search_input'] 变量的排序和搜索功能.用户在使用 GET 的表单上输入他们的搜索条件.然后将变量放入 url 中,以便显示正确的结果.

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.

我遇到的问题是,每当我单击分页链接时,它都会将 $_GET['page'] 变量添加到 url 的末尾并删除 $_GET['searchby']$_GET['search_input'].当我提交搜索表单时,它添加了 $_GET['searchby']$_GET['search_input'] 但删除了 $_GET['page'].

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

如何使用锚标记和搜索/排序表单将 GET 变量添加到当前页面 url 的末尾,而不删除任何现有的 GET 变量,但如果它们是相同的 GET 变量名称,则覆盖它们?

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?

推荐答案

试试这个:

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>

请注意,$_SERVER['REQUEST_URI'] 为您提供当前 url,包括查询字符串值.

Note that $_SERVER['REQUEST_URI'] gives you current url including query string value.

相关文章