sykatch
sykatch

Reputation: 301

Redirect to user defined location

I'm trying to write a form in which user writes a location and the page redirects there after submit. How can I accomplish that? I have problem to find out how to put something to action="" when I do not know the value yet. Something with this logic:

 <form action="how-to-get-this-value">
   <input name="location" type="text">
   <input name="submit" type="submit">
 </form>

I can use whatever I want to do this (javascript, css, php...).

Upvotes: 0

Views: 40

Answers (2)

Koen Hollander
Koen Hollander

Reputation: 1711

There are a lot of good answers for this question. But I advise you to do the following...

Create a HTML form like this:

Them submit it to the php page with this. This can also be the same page.

Upvotes: 0

kzhao14
kzhao14

Reputation: 2630

You don't need to make a <form> to do this. Forms are used more often for processing data. Instead, you can use Javascript. There are many ways to do this, but if you want to use a <form>, the easiest way is to redirect the user when they submit the form as follows:

function red() {
  var url=document.getElementById('url').value;
  window.location.href=url;
}
<form id="form">
  <input id="url" type="text">
  <input type="submit" onClick="red(); return false" value="Submit">
</form>

Of course, you would probably want to add verification methods to make sure the user gets to where they want to get (ie typing "google.com" would take them to "https://www.google.com/" instead of "yourdomain.com/google.com/").

Alternatively, you can simply remove the <form> tags, and it will work the same.

function red() {
  var url=document.getElementById('url').value;
  window.location.href=url;
}
  <input id="url" type="text">
  <input type="submit" onClick="red()" value="Submit">

Upvotes: 2

Related Questions