【发布时间】:2013-06-26 18:52:44
【问题描述】:
http://example.com/bills.php?sort=highest
http://example.com/bills.php?sort=alpha
http://example.com/bills.php?sort=lowest
所以上面的网址按最高、字母和最低排序。他们将 $_GET 方法与表单一起使用。我想知道是否有办法在不更改 url 的情况下提交表单/获取 $_GET 数据?
<form action="bills.php" method="get">
<select onchange="this.form.submit();" name="sort" class="right sort">
<option>What would you like to sort by?</option>
<option value="highest" <?php if(isset($_GET['sort']) && $_GET['sort'] == 'highest'){?> selected="selected" <?php } ?>>Highest to Lowest</option>
<option value="lowest" <?php if(isset($_GET['sort'])&& $_GET['sort'] == 'lowest'){?> selected="selected" <?php } ?>>Lowest to Highest</option>
<option value="alpha" <?php if(isset($_GET['sort'])&& $_GET['sort'] == 'alpha'){?> selected="selected" <?php } ?>>Alphabetically</option>
</select>
</form>
上面是我能够提交新排序请求的代码。
我想让我的网址保持在example.com/bills.php 这可以吗?
这可以使用 .htaccess 重写 url 吗?我尝试了一种 URL 重写方法,它允许我访问 http://example.com/bills/alpha,它工作正常,但是当我选择一个新查询时,它变成了 http://example.com/bills/bills/?sort=alpha。
这是我的 .htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^bills/(.*)$ /bills.php?id=$1
【问题讨论】:
-
使用表单方法作为post,并将值保留在同一表单下的隐藏字段中
-
你有什么理由不使用post吗?
-
我希望能够通过输入变量来使用变量进行查询。我只是想知道是否有任何方法可以使用 $_GET 并获得 url查询完成后恢复为 bills.php。如果没有,我想知道如何采取替代方案并使用 URL 重写。
标签: php url-rewriting get