Forum Moderators: open

Message Too Old, No Replies

Variables in links from input forms

inserting text in an input field, activating a specific hyperlink

         

Nicholson

1:17 am on Jun 27, 2006 (gmt 0)

10+ Year Member



I am trying to find a way (google search found nothing)to have a text field on my website, so that the input will be inserted into a hyperlink, then activated...

For example: text input="forum", submit button to activate hyperlink=http://www.website.com/folder/forum...

eelixduppy

1:32 am on Jun 27, 2006 (gmt 0)



This can be accomplished with JavaScript. Something like this maybe:

<script language='javascript'>
function change_url() {
document.location = "http://www.website.com/folder/"+form1.text1.value;
}
</script>

<form name='form1'>
<input type='text' name='text1' />
<input type='button' value='Go to URL' onClick='change_url()' />
</form>

Good luck!

Nicholson

10:58 pm on Jun 27, 2006 (gmt 0)

10+ Year Member



OK, I got it sort of working, however, by habit, i hit enter after i have typed what i want. When I hit enter, the page doesn't change, but the address says this "file:///C:/index.html?text1=kboi" kboi being my variable. When i actually click on "Go to URL" it works. How can i make it submit properly when i hit enter? And, what is the code to open it in a new window?

eelixduppy

12:59 am on Jun 28, 2006 (gmt 0)



Umm...I think if you change the code to the following it will work:

<form name='form1' onSubmit='change_url()'>
<input type='text' name='text1' />
<input type='button' value='Go to URL' onClick='change_url()' />
</form>

I'm not an expert when it comes to javascript but I think this should do it. ;)

Good luck!

Nicholson

3:06 am on Jun 28, 2006 (gmt 0)

10+ Year Member



Hitting enter still doesn't submit, but i guess I can live with it, do you know how to make it open in a new window?

rocknbil

8:04 pm on Jun 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try changing the button type to a submit button and return false on the form. To make a new window, set the target to new window.

<form name='form1' onSubmit='change_url(); return false();' target="new window">
<input type='text' name='text1' />
<input type='submit' value='Go to URL' onClick='change_url()' />
</form>

This does two things. One, it should allow it to submit when you hit enter, and two, it will allow the form to still be submitted if javascript is disabled.

Which it currently will not. :-)

Of course, if that's the case, the whole thing won't work and you need to use some server-side language such as perl or php to make it work, but that's another topic.

Nicholson

2:48 am on Jun 29, 2006 (gmt 0)

10+ Year Member



Ok... I put in that new code, and...

Before that change, enter didn't work, but clicking on submit worked.. now...

I hit enter and a new window opens, but still the same page, and this is the address again "file:///C:/index.html?text1=kboi"... then the original page actually goes to the intended website... kinda weird and obviously something is wrong..

P.S. thank you guys for helping me...