Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html button v.s. html submit?

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

like image 452
George2 Avatar asked Dec 30 '09 10:12

George2


People also ask

What is the difference between button and submit button in HTML?

A 'button' is just that, a button, to which you can add additional functionality using Javascript. A 'submit' input type has the default functionality of submitting the form it's placed in (though, of course, you can still add additional functionality using Javascript).

What is difference between in submit button and button?

In HTML, <input type="button" /> is used to create buttons in an HTML form. Inside the <button> tag, you can place content like text or images. But, this is not the case with the buttons created with <input> tag.

Should I use Submit or button?

Both <button type="submit"> and <input type="submit"> display as buttons and cause the form data to be submitted to the server. The difference is that <button> can have content, whereas <input> cannot (it is a null element).

Is submit a button?

The Submit ButtonThe <input type="submit"> defines a button for submitting the form data to a form-handler. The form-handler is typically a file on the server with a script for processing input data. The form-handler is specified in the form's action attribute.


1 Answers

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

like image 108
Sarfraz Avatar answered Sep 19 '22 01:09

Sarfraz