-1

What I am trying to do is have users enter a certain code assigned to them into a box, and when they do so, they are forwarded to a different page which changes for each code. I need to have multiple different codes that go to multiple different pages.

For example - entering the code "1" would forward someone to google.com, and entering the code "2" would forward someone to bing.com.

Is this even possible, and if so, how would I do this?

j08691
  • 204,283
  • 31
  • 260
  • 272
  • 2
    Yes, it's possible. Example: `function foo(code){document.location.href=\`http://${['foo.bar', 'www.google.com', 'bing.com'][code]}\`||document.location.href;}` and call `foo` with `parseInteger(input.value)` as the code – xxMrPHDxx Mar 02 '20 at 02:47

2 Answers2

2

You could use an onclick Javascript to redirect the user to the targeted page

Then get the code and redirect them to the intended website

Example:

function redirectUser() {
  var code = document.getElementById("code").value;
  if(code == "0"){
    window.location.href = "https://www.w3schools.com/";
  }
  else if(code == "1"){
    window.location.href = "https://www.google.com/";
  }
  else if(code == "2"){
    window.location.href = "https://www.bing.com/";
  }
  else{
    alert("Error: Code not found");
  }
}
Enter code here: <input type="text" id="code" /> <br/>
<button type="button" value="Go" onclick="redirectUser();">Go</button>

For more information on how to redirect using javascript, see this link: How do I redirect to another webpage?

  • is it possible to make it say something if the code does not exist? – ourmoneycolorado Mar 02 '20 at 03:31
  • Yes, you could use an else statement. I would recommend you look at https://www.w3schools.com/js/ for more information on how javascript works, as it may help you learn a bit. Note: Updated answer base off of your feedback – Joshua Proctor Mar 02 '20 at 03:35
0

You may this code and edit around good luck

<p>Entering the code "1" would forward someone to google.com, and entering the code "2" would forward to bing.com.</p>

<!DOCTYPE html>
<html>
<body>
<input id = "code" type = "text" name = "Code"
      placeholder = "Enter a Code here">
<input type = "submit" name = "button" onclick = "logic()">
<script>
function logic() {
 var code= document.getElementById("code").value;

 if(code == 1){
  window.location.replace("http://google.com");
 }else if(code == 2){
  window.location.replace("http://bing.com");
 }

}
</script>
</body>
</html>
Dinesh
  • 43
  • 1
  • 6