CL So
CL So

Reputation: 3759

How to make sure form's relative URL works?

This is my webpage

www.domain.com/product/1

To update the product, I should submit to this URL

www.domain.com/product/1/update

In the product page, I have these forms to submit data

<form method="post" action="update">
    <input type="submit" />
</form>

<form method="post" action="./update">
    <input type="submit" />
</form>

Both forms do not work, because it will submit to

www.domain.com/product/update

They only work if URL is

www.domain.com/product/1/

If I hard code the product id in action

<form method="post" action="1/update">
    <input type="submit" />
</form>

Then www.domain.com/product/1/ will not work, because it will submit to www.domain.com/product/1/1/update

What value should the action be to make sure the form can submit to correct URL?

If it is possible, I don't want to render absolute path or product id in form action.

Upvotes: 0

Views: 229

Answers (3)

Shuggs
Shuggs

Reputation: 58

You're having trouble because the action path product/1 is treated like a file not a directory, if the 1 had a slash after domain.com/product/1/ then you wouldn't have any problems. Like @sumitb.mdi suggested a base meta tag will work.

You're possibly using a framework, you might be able to utilise a URL builder function for 'named routes' or something.

Upvotes: 0

Shaggy
Shaggy

Reputation: 6796

As it stands to ensure this works in all situations you will need to use the product ID, like so: /product/1/update.

Another option, though, would be to make a couple of tweaks server side to make sure directory URLs always have a trailing slash.

Upvotes: 1

Quentin
Quentin

Reputation: 943537

You cannot achieve what you are asking for.

You have to specify either an absolute path or the product id.

You could generate the the product id programatically though.

Upvotes: 0

Related Questions