sykatch sykatch - 17 days ago 6
PHP Question

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...).

Answer

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">