开发者

Redirect user to directory based on form input

I know enough about the coding end of web design to be embarrassed by what I don't know. What I want to do is to have various print promotions in newspapers and what not along the lines of: for more information please visit www.mysite.com/2345.

If the visitor doesn't enter the entire url in the nav bar and ends up at the main index, I want to have a text field there so they can enter "2345", hit enter or submit, then be redirected to www.mysite.com/2345 wherein the folder's index page will load.

I usually search and find the coding info I'm looking for, but I can't figure out a concise way to search this particular problem. Can anyone help with this or point me in the right direction for help elsewhere?

开发者_高级运维

Thanks.


Pretty simple with JavaScript, here's a working example:

<form onsubmit="location.href='http://www.mysite.com/' + document.getElementById('myInput').value; return false;">
  <input type="text" id="myInput" />
  <input type="submit" />
</form>


You can do it using javascript. Here's a terribly ugly example but should give you an idea.

<form>
<input type="text" id="number" name="number" />
<input type="submit" onclick="window.location = window.location + '/' + number.value; return false;"/>
</form>

Ideally you'd also handle it in whatever server side language you're using as well. Here's a PHP example:

<?
if(isset($_POST['number'])){
    header('Location: http://www.yourdomain.tld/'.$_POST['number']);
    exit;
}
?>


Very simple example with PHP, so that you can understand how it works. Very simple.

<?php
if (isset($_POST['bt']))
{
    header("Location: http://localhost/" . $_POST['folder']);
}
?>
<html>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="folder" id="folder" />
    <input type="submit" name="bt" id="bt" value="Go To" />
</form>
</html>

This is you index.php File, in your htdocs/www folder.


The PHP notices when you click the button and it will redirect you to

http://www.yourdomain.com/what-you-have-writen-in-the-textfield

You can also do it with JavaScript, but with PHP it will work even if your visitor browser has JavaScript disabled.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜