George2
George2

Reputation: 45771

javascript http get operation issue

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

Upvotes: 0

Views: 353

Answers (4)

James Polley
James Polley

Reputation: 8181

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

<form method=GET action="http://testsearch/results.aspx">

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

Upvotes: 5

DoctorLouie
DoctorLouie

Reputation: 2674

That doesn't require javascript, just a simple html form.


<form method="get" action="http://testsearch/results.aspx">
<input type="text" id="k" name="k" />
<input type="submit" id="Go" value="Search" /> 
</form>
This will do the trick.

Upvotes: 4

David Hedlund
David Hedlund

Reputation: 129792

<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

Upvotes: 1

rahul
rahul

Reputation: 187030

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;

Upvotes: 1

Related Questions