如何防止在URL中显示空的GET变量 [英] How do I prevent empty GET variables from displaying in the URL

查看:90
本文介绍了如何防止在URL中显示空的GET变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一些奇怪的情况。我有一个使用GET方法提交搜索功能的表单。在搜索后的后续页面上,所有变量都显示在URL中,即使它们为空。例如,如果我搜索电影标题等于你好,我会得到这个:



/ GetResults?title = hello& year =&导演=& firstname =& lastname =



这是正常还是我做错了什么?这里是我使用的表单:

 < form action =/ FabFlix / servlet / GetResultsid =search- formmethod =getaccept-charset =utf-8> 
< p>电影标题:< / p>< input type =textname =title/>
< br />
< p>年份:< / p>< input type =textname =year/>
< br />
< p>导演:< / p>< input type =textname =director/>
< br />
< p> Star的名字:< / p>< input type =textname =firstname/>
< br />
< p> Star's Last Name:< / p>< input type =textname =lastname/>
< br />
< br />
< input type =submit/>
< / form>


解决方案

这很正常。为了防止这种行为,请考虑在表单上手动组装URL并重定向的onsubmit处理程序。如果你这样做,不要忘记测试这与JavaScript启用和禁用,以确保两种情况下仍然工作正常。


I'm having a bit of a weird situation here. I have a form that submits using the GET method for a search function. On the subsequent page after a search, all the variables are displayed in the URL even if they are empty. For example if I make a search for movie title equaling "hello," I'll get this:

/GetResults?title=hello&year=&director=&firstname=&lastname=

Is this normal or am I doing something wrong? Here is the form I am using:

<form action="/FabFlix/servlet/GetResults" id="search-form" method="get" accept-charset="utf-8">
<p>Movie Title:</p><input type="text" name="title"/>
<br/>
<p>Year:</p><input type="text" name="year"/>
<br/>
<p>Director:</p><input type="text" name="director"/>
<br/>
<p>Star's First Name:</p><input type="text" name="firstname"/>
<br/>
<p>Star's Last Name:</p><input type="text" name="lastname"/>
<br/>
<br/>
<input type="submit"/>
</form>

解决方案

This is normal. To prevent this behavior, consider an onsubmit handler on your form which assembles the URL manually and redirects. If you do this, don't forget to test this with javascript both enabled and disabled to make sure both scenarios still work OK.

这篇关于如何防止在URL中显示空的GET变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆