Form method = “get” pretty URL

为君一笑 提交于 2019-12-12 17:47:54

问题


So I'm using this HTML form for a simple search field

<form action = "/search/" method = "get">
<input type = "text" name = "search_term"/>
</form>

But then when it processes in the URL it's like this

http://mysite.com/search/?search_term=something

I know this is perfectly normal, but is there a way to get it to send like this?

http://mysite.com/search/something

I will then use .htaccess in the search directory to rewrite the url.


回答1:


Do you want just the URL to be pretty or you want the parameters to be hidden from the URL. You can change your method='post' and it'll pass all the parameters as part of the HTTP Header and completely hidden from your user. If you want to make the URL look nicer, you should look at mod_rewrite and .htaccess files on how to change a search query from ?search_terms=something to /something




回答2:


You would have to use javascript to hook into the submit event of the form, or the click event of some button that submits the form, etc.

You would then redirect the page after constructing the url based on the inputs in the form.



来源:https://stackoverflow.com/questions/10622787/form-method-get-pretty-url

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