0

I wanted to be able to pass an id in the action of the form example: action="/numeric id/prefix" precisely the numeric id must be a variable taken from the current url

I DON'T USE JQUERY

i try this:

<form id="prefix-form" method="post"  action="javascript:redictURL('prefix')">
                        <label for="prefix">Pre</label>
                        <hr></hr>
                        <input type="text" name="prefix" id="email" placeholder="&&prefix&&" required>
                        <input type="submit" value="" id="buttonPrefix">
                    </form>

the function "redictURL" take the url and add /prefix at the end:

const redictURL = (data) => {
                let id = location.pathname.split('/')[1]
                return `/${id}/${data}`
            }

,but what comes back to me in the end is this and so it doesn't send a post request to the server like I would like

Heiko Theißen
  • 12,807
  • 2
  • 7
  • 31
PXL_Fl4me
  • 3
  • 2
  • Does this answer your question? [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) Note that the accepted answer’s original solution uses plain JavaScript. I recommend the first update above the original solution. – The Empty String Photographer Aug 07 '23 at 12:19

1 Answers1

0

If you need to set the action attribute of a form dynamically, you can do so in the onsubmit event:

<form onsubmit="this.action = 'prefix'">
  <input type="submit" />
</form>

But if the action does not depend on the contents of the form, it would be better to have the server set the correct value directly when the form is rendered.

Heiko Theißen
  • 12,807
  • 2
  • 7
  • 31