Home > Software engineering >  <form> action not being relative
<form> action not being relative

Time:06-01

This is a pretty basic question :D

I have an HTML at localhost/book which has a form,

    <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? , so clicking Search should go like localhost/book/d?q=some data

  • But instead, it goes like localhost/d?q=some data.

  • How do I make it goes to localhost/book/d

CodePudding user response:

For being relative to parent directory, please use

action = "../d"

CodePudding user response:

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. 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. This could be done by rewrite on web-server level. 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.

  • Related