views:

66

answers:

4

Hello everyone,

I am new to Javascript and try to find a solution to my issue but failed. My problem is, 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, any solutions?

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

  <input type="text" id="k" name="k" />
  <input type="submit" id="Go" value="Search" />

thanks in advance, George

+1  A: 

Use

window.location

to go to the new location.

To get the textbox value you can use

document.getElementById ( "k" ).value;

and the whole code will be

var textValue = document.getElementById ( "k" ).value;

window.location = 'http://testsearch/results.aspx?k=' + textValue;
rahul
If you would use this way, you should urlencode the valuevar textValue = encodeURIComponent(document.getElementById ( "k" ).value);but, for this case, you should rather use <form> element.
Rafael
+1  A: 
<input type="button" id="Go" value="Search" onclick="location.href = 'http://testsearch/results.aspx?k='+document.getElementById('k').value;" />

Note that it's a button, not a submit

David Hedlund
Why submit does not work?
George2
Thanks! Question answered!
George2
+4  A: 
drlouie - louierd
This is the correct answer. Unfortunately it won't work because it seems this is an ASP.NET application where you can have only a single form per page, the method is set to `POST` and you don't have much control over the action. I suppose the original poster should have tagged this question with `asp.net`.
Darin Dimitrov
ASP.NET cannot possibly be so bad that you can't turn off the "The entire page is a single form" feature, can it? If you really need to use JavaScript to get a form this simple to work, then your underlying system is hidiously broken and needs to be fixed.
David Dorward
Even if the ASP.NET helpers for forms are so limited, surely it's still possible to have the code dump some raw HTML into the output?
James Polley
@James ASP.NET has a tenancy to wrap the entire content of the `body` in a form - and you can't nest forms.
David Dorward
Owch. That's nasty.
James Polley
David Dorward
+5  A: 

You don't need javascript for this, just change the form method from POST to GET:

<form method=GET action="http://testsearch/results.aspx"&gt;

The "?k=StackOverflow" will magically be appended. It's what form method GET does.

James Polley