开发者

URL Redirect based on input but if input url does not exist send back to same page

开发者 https://www.devze.com 2023-03-27 16:56 出处:网络
This code is almost perfect for what I need: <form onsubmit=\"location.href=\'http://www.mysite.com/\' + document.getElementById(\'myInput\').value; return false;\">

This code is almost perfect for what I need:

<form onsubmit="location.href='http://www.mysite.com/' + document.getElementById('myInput').value; return false;">
  <in开发者_如何学Goput type="text" id="myInput" />
  <input type="submit" />
</form>

The only thing missing is if they user puts in an input that does not exist I want it to take them nowhere or redirect back to the same page. Right now it takes them to an error page and they have to use the back button to go back and try again.

Any help will be greatly appreciated.

Thank you


Could you perhaps write a small PHP script that you use as the form action? Inside this script, you could check the supplied user input and validate that the URL exists. If it does, you could do a header() redirect to the given page. If it does not exist, you could redirect them to the desired page.

<?php
$input = $_POST['myInput']; // You'll also need to add in a name='' tag in the HTML!
$desired_page = ''; // page you want to go to if inputted file cannot be found

if (file_exists($input)) {
  header("Location: $input");
}
else {
  header("Location: $desired_page");
}

It'd need a lot of refining, but that's the idea. Would that do what you want?

0

精彩评论

暂无评论...
验证码 换一张
取 消