简体   繁体   English

<form>动作不是相对的

[英]<form> action not being relative

This is a pretty basic question :D这是一个非常基本的问题:D

I have an HTML at localhost/book which has a form,我在 localhost/book 有一个 HTML,它有一个表单,

    <form action="d" method="get">
      <input name="q" placeholder="Book name"></br>
      <button type="submit">Search</button>
    </form>
  • The d action meant to be relative to current URL, which localhost/book, right? d 操作意味着相对于当前 URL,即 localhost/book,对吗? , so clicking Search should go like localhost/book/d?q=some+data ,所以点击搜索应该像localhost/book/d?q=some+data

  • But instead, it goes like localhost/d?q=some+data .但相反,它就像localhost/d?q=some+data

  • How do I make it goes to localhost/book/d我如何使它进入localhost/book/d

对于相对于父目录,请使用

action = "../d"

This is a URL-Issue: Because your current URL is lacking a trailing slash (/) the last part of the URL is not treated as a folder but a "file" instead.这是一个 URL 问题:因为您当前的 URL 缺少尾部斜杠 (/),所以 URL 的最后一部分不被视为文件夹,而是被视为“文件”。 So if you submit the form it will go on level up in your case.因此,如果您提交表格,它将在您的情况下升级。

One solution would be to add a trailing slash in the URL.一种解决方案是在 URL 中添加尾部斜杠。 This could be done by rewrite on web-server level.这可以通过在 Web 服务器级别重写来完成。 Or you could change the link, but in that case you are never safe if the visitor is typing a slash or not.或者您可以更改链接,但在这种情况下,无论访问者是否输入斜线,您都不会安全。

The safer solution would be not to use a relative URL in the action of the form.更安全的解决方案是不在表单操作中使用相对 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM