Forum Moderators: open

Message Too Old, No Replies

HTML Form Field Site Redirect

         

tkimage

10:57 pm on Oct 23, 2008 (gmt 0)

10+ Year Member



I am looking for a VERY SIMPLE, yet cross browser supported, code that will allow visitors to my site to enter text into a form field and upon submission be redirected to the directory name they entered.

Let me explain.

One field form with SUBMIT button.
The visitor is at www.example.com
They enter "text" into the form field and click SUBMIT.
The code redirects them to www.example.com/text

Thank you for your help.

[edited by: tedster at 11:07 pm (utc) on Oct. 23, 2008]
[edit reason] switch to example.com - it can never be owned [/edit]

coopster

11:48 pm on Oct 23, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, tkimage.

Quite simple, really. You create the form with a method attribute that reads the text entered and either redirects them to the new directory index page or returns a message that an invalid directory has been entered. Which server-side language are you using?

tkimage

3:28 am on Oct 24, 2008 (gmt 0)

10+ Year Member



I have access to the following: CGI, SSI, PHP 5, PERL 5, Apache ASP

Thank you for your assistance.

Trace

1:20 pm on Oct 24, 2008 (gmt 0)

10+ Year Member



Or maybe a simple JavaScript would be sufficient?
<form action="" method="">
<fieldset>
<legend>The form</legend>
<input type="text" id="addy">
<input type="button" id="theButton" value="Go!">
</fieldset>
</form>

<script type="text/javascript">
document.getElementById('theButton').onclick = function(){
document.location.href=document.getElementById('addy').value;
}
</script>

tkimage

4:52 am on Oct 26, 2008 (gmt 0)

10+ Year Member



Thank you "Trace" the JavaScript works great. I tested it in IE, FF, and Safari.

However, when I try to replace type="button" with type="image" and the neccessary info to get my image to be viewed, it quits working? Why?

Also, when you hit the "Enter/Return" on the keyboard, it doesn't work, but if you click the button with the mouse it does. Why?

Your help is greatly appreciated.

syktek

10:56 pm on Oct 26, 2008 (gmt 0)

10+ Year Member



tkimage,

type="image" will trigger the form action so if you want to use onclick you should use type="button". if you need a custom image just use the <img> tag.

enter/return on a form field will also trigger the form action, if you put the focus (tab to) the button then press enter/return it should act as you expect.