所以我为一个表单(搜索表单(编写了这段代码,它看起来像这样:
<form action="" method="get">
<select >
<option value="name">Client Name:</option>
<option value="email">Client Email:</option>
<option value="tnx_id">Transaction ID:</option>
<option value="amount">Amount:</option>
</select>
<input type="text" name="svalue" placeholder="Search"></input>
<input type="image" name="submit" src="img/search_btn.png"></input>
</form>
当用户单击"搜索"时,我希望 URL 的格式如下:
http://localhost/pp/index.php?search&name=haha
但相反,我得到:
http://localhost/pp/index.php?search=name&svalue=haha&submit.x=0&submit.y=0
我只是在正确格式化网址时遇到困难,因为其余的代码很好,我无法摆脱最后的那个&submit.x=0&submit.y=0
网址也是。伙计们,你有什么想法吗?基本上我想要的 url 格式会得到选定的选项(姓名、电子邮件、tnx_id、金额(并为其分配 svalue 的值。
我很感激你的帮助,提前谢谢你,谢谢。
这就是表单的工作方式。每个控件在提交的数据中给出一个名称=值对。
你可以用JavaScript破解它,但这变得很脆弱。为 <select>
元素命名并更改服务器端脚本,以便它将两条数据片段作为单独的数据片段。
我也无法摆脱网址末尾的
&submit.x=0&submit.y=0
从图像映射中删除 name
属性,使其不会成为成功的控件。
<input type="image" src="img/search_btn.png">
您应该进行的其他改进:
-
<input>
是一个空元素。它不应该有结束标记。如果你正在编写XHTML,那么使用空元素语法。 -
placeholder
不应用作<label>
的替代品 - 将 alt 属性添加到图像映射
使用 JavaScript 创建 URL。 如果你使用的是jQuery:
var url = 'http://localhost/pp/index.php?search&'
+ encodeURIComponent($('#id-of-your-select-element').val())
+ '='
+ encodeURIComponent($('#id-of-your-svalue-element').val());
编辑:至于摆脱submit.x
和submit.y
,请参阅@Quentin的答案。
在你的 PHP 代码中,你可以添加这个:-
if(isset($_GET['name']))
{
$searchString="&cs=".$_GET['name'];
}
然后使用这个:
http://localhost/pp/index.php?".$searchString."/
我认为这将帮助您获得正确的东西。 如果您想使用它进行分页,我有一个我以前使用过的代码,它效果很好。 试试一次!