入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

get请求表单的action属性后不能带参数

创建时间:2016-06-22 投稿人: 浏览次数:5463

最近在编写网页程序时,有类似如下代码:

 <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属性中不能附带任何参数,如果需要附加额外的参数,可以采用如下方式:

  1. 采用POST请求方式,在form中增加属性method="post"即可。
  2. 如果仍然想使用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>

原文地址:http://www.365mini.com/page/get-method-form-conflict-with-action-which-has-parameter.htm
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。