How do I prevent empty GET variables from displaying in the URL

强颜欢笑 提交于 2019-12-13 00:33:06

问题


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>

回答1:


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.




回答2:


I believe that's normal operation for GET. Do you have to use GET instead of POST?

It seems that a relatively simple change to get the variables from not displaying in the URL is to use the POST method instead of GET.



来源:https://stackoverflow.com/questions/1561798/how-do-i-prevent-empty-get-variables-from-displaying-in-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!