tags:

views:

230

answers:

6

Hello everyone,

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;"/&gt;

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;"/&gt;

thanks in advance, George

+4  A: 

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

Sarfraz
Why submit is not working in my scenario? I want to know how browser handles submit differently from handles button, so that it makes submit does not work in my scenario?
George2
you need to put return false; statement at the end of onlcick event of submit button so that it does not submit, rather it performs action told in onclick event.
Sarfraz
Sorry, I forget to add onclick event in my code. Just corrected. What are the differences between submit and button in my case and why submit does not work? In my original code, there is no Form element.
George2
the submit button can be used the submit the form (you know a form is one with <form> tag) while a normal button can be used to execute some javascript code. In your case, you should use normal button if you want to redirect the page. :)
Sarfraz
To make submit button work, we have to include it in a form?
George2
@George: Yes you need to put them in forms
Sarfraz
+4  A: 

Hi George2,

<button>-elements and <input type="button"/> don't do anything by default, unless you tell them to do something with Javascript.

<input type="submit"/> will submit the form it is in.

So, if <input type="submit"/> won't work, you got it probably not in the <form/>-element itself.

Harmen
Sorry, I forget to add onclick event in my code. Just corrected. What are the differences between submit and button in my case and why submit does not work? In my original code, there is no Form element.
George2
To make submit button work, we have to include it in a form?
George2
Yes, wrap the `<input/>`-elements in `<form action="result.aspx" method="get"> </form>`
Harmen
+2  A: 

<button> means "put a button in the page and do whatever the onclick event says". So if you don't write an onclick handler the page doesn't do nothing.

If you use submit is ok, because you want to redirect to another page.

If you want to use button anyway you can do this way:

<script>
function doTheSearch() {
  // do the submit mannually
  document.getElementById('myForm').submit();
}
</script>
<form id="myForm" action="results.aspx">
 <input type="text" id="k" name="k" />
  <input type="button" id="Go" value="Search" onclick="doTheSearch();" />
</form>

Warning: submit button with onclick

If you have a submit button (inside a form, it is, a working submit button) with an onclick event, some browsers will:

1) execute onclick

2) execute submit

your onclick tries to redirect but the submit button wins.

If you want to avoid it you have some options:

a) change submit button to normal button

b) avoid the submit thing (add onsubmit="return false;" to form element)

c) use the submit procedure (form action="..." method="get", no onclick event), the browser will be happy and you can control the submit in the onsubmit event (you can cancel it or not).

helios
Sorry, I forget to add onclick event in my code. Just corrected. What are the differences between submit and button in my case and why submit does not work? In my original code, there is no Form element.
George2
I added a warning that's very important (I debugged a similar situation some time ago). I think it answer the question.
helios
Also the onclick event of a submit button will not get fired if the user submits the form by hitting enter in a text field.
robertc
If fact, that's a good criteria to choose between a submit button and a normal button ("Do I want to send the query vía Enter key inside the form?")
helios
To make submit button work, we have to include it in a form?
George2
Yes. Because submit means "send a request to the URL indicated in <form action="...."> using method indicated in <form method="..."> (GET or POST) and include all the data of the form. You can have multiple forms in a page, each with its submit button and its data. NOTE: if you want to use a submit button as a normal button you can (it has onclick event anyway) but you should use <input type="button"...> instead.
helios
+4  A: 

Hi George,

If that's the only field in your form, simply set the form's method to "get" and it'll work.

<html>
<body>
    <form action="http://localhost/mytest" method="get" >
        <input type="text" id="k" name="k" />
        <input type="submit" id="Go" value="Search" />
    </form>
</body>
</html>
Asaf R
Sorry, I forget to add onclick event in my code. Just corrected. What are the differences between submit and button in my case and why submit does not work? In my original code, there is no Form element.
George2
If you want to do it with JavaScript I'd suggest looking at @Sarfraz Ahmed answer. But, unless you have a good reason to use JavaScript there, I'd go for plain HTML (such as in my answer).
Asaf R
To make submit button work, we have to include it in a form?
George2
As best I can tell - yes you have to include it in a form(to say the least - that is the norm)
Asaf R
+1  A: 

Hi George, make sure you got the input's in a form tag with a GET method:

<form action='http://testsearch/results.aspx' method='GET'>
  ... inputs
</form>
Pim Jager
Sorry, I forget to add onclick event in my code. Just corrected. What are the differences between submit and button in my case and why submit does not work? In my original case, there is no form element.
George2
+1  A: 

If I'm understanding correctly, it is not working because it is not in a form tag. If you put it in a form tag with method="get" it should work. The button works because it does not have to be in a form.

Evan Huddleston
Sorry, I forget to add onclick event in my code. Just corrected. What are the differences between submit and button in my case and why submit does not work? In my original code, there is no Form element.
George2