1

I'm a making a form but clicking on that form's submit button doesn't take me to the right path.

<form action="delete" method="post">
 <button type="button">Cancel</button>
 <button type="submit">Delete</button>
</form>

It should take me to http://localhost:5000/<id>/delete (In place of will be the user ID)

But it takes me to http://localhost:5000/delete

And http://localhost:5000/<id>/delete is a webpage not a file. Which means that this web site is not static, it's dynamic.

What am I doing wrong here?

asked Jun 1, 2022 at 11:30
2
  • It will lead you to localhost:5000/id/delete only if your current URL is localhost:5000/id. Try to put this in action: "/id/delete" Commented Jun 1, 2022 at 11:56
  • @Moebius The id in URL is actually a placeholder for user ID. I thought it's not relative to the question so I didn't add it before but now I updated the question. Commented Jun 1, 2022 at 12:16

3 Answers 3

2

The behavior @Tuilip experienced is because of the relative path in the form action attribute. The relative path in action will always replace anything after the last slash in the URL.

Example for the form action "delete":

  • If the URL is "/items/123" - the result will be "items/delete"
  • If the URL is "/items/123/" – the result will be "items/123/delete"
Iurii Tkachenko
3,19931 silver badges36 bronze badges
answered Jan 7 at 13:26
Sign up to request clarification or add additional context in comments.

Comments

1

You should try to extract the ID from the URL and then set that ID into your form action like:

follow this stackoverflow question to extract ID from url How can I get query string values in JavaScript?

<form action="/<id>/delete" method="post">
 <button type="button">Cancel</button>
 <button type="submit">Delete</button>
</form>
answered Jun 1, 2022 at 12:16

Comments

1

Note: Assuming your delete is a file and it is in the same directory (id/) as your current html file.

A little thing about paths:

  • pathname is absolute. This means it will search in the root directory for pathname. (in this case the root folder of your webapp)
  • ./pathname is relative. The . means the current directory (aka the directory your file (e.g. index.html) is in).
  • ../pathname is also relative. The .. means one folder over the current folder you are in.

Example:

rootFolder/
 test.html
 anotherFolder/
 otherFile.html
 folder/
 index.html
 delete.html

Now assuming you are in folder/

  • test.html -> Valid
  • ./test.html -> Does not exist since . == rootFolder/folder/
  • ../anotherFolder/otherFile.html -> Valid, since .. goes one folder up (from folder/ to rootFolder) and then enters anotherFolder/otherFile.html

The exact same principles also apply when you open your terminal and cd (change directory) around. You can try out that the path works there first.

To answer your question, it should be: action="./delete" you are using an absolute path right now.

answered Jun 1, 2022 at 11:56

2 Comments

Oh sorry I forgot to specify in my question that delete is a page and not a file. I'll edit my question now.
pathname is absolute — No, it isn't. That's /pathname you're thinking of. pathname and ./pathname are equivalent.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.