Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
123
rated 0 times [  127] [ 4]  / answers: 1 / hits: 30250  / 15 Years ago, wed, december 30, 2009, 12:00:00

I have an input text box and a search submit button, and when user clicks the Search submit button, I want to redirect user to url http://testsearch/results.aspx?k=<value of text box k>, for example, if user put StackOverflow into text box and then clicks the search button, I want to redirect user to the following page,



http://testsearch/results.aspx?k=StackOverflow



I find when I use button for Search button, it works (see below source codes),



  <input type=text id=k name=k />
<input type=button id=Go value=Search onclick=location.href = 'http://somemachine/Search/results.aspx?k='+document.getElementById('k').value;/>


but when I use submit for Search button, it does not works (see below source codes), why?



  <input type=text id=k name=k />
<input type=submit id=Go value=Search onclick=location.href = 'http://somemachine/Search/results.aspx?k='+document.getElementById('k').value;/>


thanks in advance,
George


More From » html

 Answers
61

You can even use the submit button this way:



 <input type=submit id=Go value=Search onclick=document.location='http://testsearch/results.aspx?k=StackOverflow'; return false; />


Semantically submit button is used to submit forms not redirect pages. You should use normal button type for this. However as i showed you can use the submit button too but that is not semantic i think.



The below line prevents the form from being submitted.



return false;


That is what you are missing in your code :)



Thanks


[#97961] Friday, December 25, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alli

Total Points: 409
Total Questions: 101
Total Answers: 105

Location: The Bahamas
Member since Tue, Apr 27, 2021
3 Years ago
alli questions
Sat, Apr 23, 22, 00:00, 2 Years ago
Mon, May 18, 20, 00:00, 4 Years ago
Tue, Mar 24, 20, 00:00, 4 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
;