如何将 GET 数据发送到同一页面而不丢失之前的 URL 到我的页面的 GET 数据?

How to send GET data to the same page without loosing the previous GET data that is the URL to my page?

我在页面:index.php?page=2 并且有 "crteria" 和 "term" 的搜索表单,但每次我提交时,它都会带我到 index.php?criteria=x&term=x 而不是 index.php?page=2&criteria=x&term=x。所以它忽略了 page=2。我也想在我所在的同一页面上提交标准和术语。这是代码:

<div id="search">
    <form name="search" action="index.php?page=2" method="get">
        <p>Search by:</p>
        <select name="criteria">
            <option>Name</option>
            <option>Last name</option>
            <option>Project name</option>
        </select>
        <br><input type="text" name="tern" placeholder="term">
        <br><button type="submit">Find`enter code here`</button>
        <hr>
    </form>
</div>

在 index.php 中,我包含了表单所在的页面,如下所示:

if(isset($_GET['page']) && $_GET['page'] == 2){
     include 'modules/search.php';
}

所以当我在 index.php?page=2 时,search.php 为我提供了您在上面看到的表格。 我知道,这很乱,这是我的第一个项目。提前谢谢你。

您可以简单地将输入隐藏参数添加到表单中并将其从操作属性中删除:

<form name="search" action="index.php" method="get">
    <input type="hidden" name="page" value="2">

如果有多个参数或参数可能会有所不同,您可以将此代码添加到 php 中以打印以下形式:

foreach($_GET as $k => $v){
    $k = addslashes($k); //or a DBMS specific escape function
    $v = addslashes($v); //or a DBMS specific escape function
    echo "<input type='hidden' name='$k' value='$v' />";
}