user3838132
user3838132

Reputation: 31

How to build a custom URL from a HTML form?

I have a HTML form like:

<form name="input" action="" method="post">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>

I need to build a URL when the submit link is clicked and send the user to it, using the username from the form in the correct position in the URL.

URL:

http://example.com/htmlchat/init.html?init_room=1&init_user=USERNAME_FROM_FORM_HERE

Can someone help (an example would be great)?

Upvotes: 1

Views: 1297

Answers (2)

Quentin
Quentin

Reputation: 943537

  1. Don't use POST when you want the data to appear in the URL
  2. Do put all the data you want in your URL in the form
  3. Get the names of your fields right
  4. Put the base URI in the action

such:

<form action="init.html">
    <input type="hidden" name="init_room" value="1">
    <label>
        Username: 
        <input name="init_user">
    </label>
    <input type="submit" value="Submit">
</form>

Upvotes: 3

Steve Tauber
Steve Tauber

Reputation: 10159

This is possible with either Javascript or a server side language such as PHP. With Javascript you want to update the action attribute.

You could use jQuery to do this.

Upvotes: 0

Related Questions