1

I want to create a simple form with only an input textbox and search button.

A person would type in the search term in the box and press search.

All that will do is take their search term and add it to the end of the url.

So if search term is "good italian food" the submit button would send the user to http://domain/find/good italian food

Whats the simplest way to do this?

Thank you!

Sudantha
16.3k45 gold badges110 silver badges162 bronze badges
asked Dec 11, 2010 at 4:21

4 Answers 4

3
<html>
 <head>
 <script type='text/javascript'> 
 function Search() {
 var keyword = document.getElementById("keyword").value; 
 var url = "http://yourwebsite.com/find/"+keyword; 
 window.location = url; 
 return false;
 }
 </script>
 </head>
 <body>
 <form name="search">
 <input type="text" name="keyword" id="keyword" />
 <input type="button" name="btnser" onclick="Search()" value="Search" />
 </form>
 </body>
</html>
answered Dec 11, 2010 at 4:37
Sign up to request clarification or add additional context in comments.

Comments

0

I'd also suggest using encodeURI() to make it 'safe'.

http://www.w3schools.com/jsref/jsref_encodeuri.asp

answered Dec 11, 2010 at 4:41

Comments

0

use javascript and html form

create a form with a button and text box in the post of the form call the javascript function to create the url with the user input text

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
 function redirect(){ 
 var s = document.getElementbyID("txt").value;
 var url="http://url.com/" + s;
 window.location = url;
 }
</script>
</head>
<body>
 <form>
 <input type=''></input>
 </form>
</body>
</html>
answered Dec 11, 2010 at 4:28

Comments

0

you could override the action attribute within the onsubmit event: Example

HTML

<form action="#" onsubmit="send(this)" method="post" target="_blank" >
 <input id="text"/>
 <input type="submit" value="submit"/>
</form>

JavaScript

function send( form){
 form.action = location.href + '/'+encodeURI(document.getElementById('text').value);
}

Also, overriding the form action, or redirecting the user, within the onsubmit event will allow you to use a true submit button.

This gives the user the ability to just hit the enter key once they finish typing to submit the search without you having to write extra logic listening for the the enter key within the textbox to submit the search.

answered Dec 11, 2010 at 4:59

2 Comments

Thanks for this. There is always a ? at the end of the url. How do i remove that?
oh wow, didn't notice that. If you set the form method to method="post" it should get rid of the ?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.