0

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

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

5 Comments

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.
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.
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 ASP.NET has a tenancy to wrap the entire content of the body in a form - and you can't nest forms.
I've avoided ASP.NET, so what I know of it comes mostly from seeing what problems other people are having with it, but I get the impression that it is very much designed to allow Windows application developers to build websites without understanding how websites work, and (as a consequence) makes it very hard to approach development from the "web" end instead of the ".NET" end. I gather that ASP.NET MVC doesn't have this issue & could be a reasonable environment for web work (I'm still avoiding it on the principle that I don't want to get to grips with Windows as a server and dev platform).
1
<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

Comments

1

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;

1 Comment

If you would use this way, you should urlencode the value var textValue = encodeURIComponent(document.getElementById ( "k" ).value); but, for this case, you should rather use <form> element.

Your Answer

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.