最近在编写网页程序时,有类似如下代码:
<form action="index.php?controller=message&method=search" method="get">
<input name="keywords" type="text" class="text" id="input" style="height:40px;" placeholder="请输入您要搜索的标题...">
<input name="" type="submit" class="btn" value="搜索">
</form>
不过我发现在后台获取参数时,一直获取不到表单action中的
method
参数值controller=message&method=search
后经查询发现,浏览器会将表单数据封装为字符串,如controller=message&method=search,然后直接附在表单的 action URL 之后。这两者之间用问号(?)进行分隔。如果GET请求的表单action属性中已经包含参数,浏览器会直接将其过滤掉,再附加form表单数据。
因此,GET请求方式的表单的action属性中不能附带任何参数,如果需要附加额外的参数,可以采用如下方式:
- 采用POST请求方式,在form中增加属性
method="post"
即可。 - 如果仍然想使用GET请求方式,可以在form表单中添加相应的隐藏文本域,例如:
<form action="index.php?controller=message&method=search" method="get"> <input type="hidden" name="controller" value="message"> <input type="hidden" name="method" value="search"> <input name="keywords" type="text" class="text" id="input" style="height:40px;" placeholder="请输入您要搜索的标题..."> <input name="" type="submit" class="btn" value="搜索"> </form>