Shiverz
Shiverz

Reputation: 688

How to append HTML form data into the URL as path and not as a query string?

So a typical HTML form with a get would look like this :

<form action="https://mywebsite.com/mypage" method="get">
  <input type="text" id="myparam" name="myparam">
  <input type="submit" value="Submit">
</form>

and takes the user to : https://mywebsite.com/mypage?myparam=value

However, I would like to know if it is possible for my form to take my user to : https://mywebsite.com/mypage/value ? Basically, adding the myparam input's value to the path at the end of the url.

Thanks in advance !

Upvotes: 0

Views: 1459

Answers (1)

muhammed ikinci
muhammed ikinci

Reputation: 747

You can create javascript function or you can declare a backend function, and return a redirect to your request route

document.querySelector('form').addEventListener("submit", function (e) {
    e.preventDefault()
    
    let param = document.querySelector('input[name="myparam"]').value

    window.location.href = 'https://mywebsite.com/mypage/' + param
})

Upvotes: 1

Related Questions