Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a form with GET method from sending the value of the submit button?

I have a simple form:

<form action="/search" method="get">
    <input type="text" name="q" value="">
    <input type="submit" name="search" value="search">
</form>

When submitting the url becomes `/search?q=Loremipsum&search=search

I really don't want that last bit, this seems pretty common problem and think it could be solved without js, but I realized that even google.com has this problem when you click on the search button. (maybe they don't care much about ugly urls?)

search?hl=en&source=hp&q=Loremipsum&btnG=Google+Search&aq=f&..

Is there a way to prevent the value of the submit button to be excluded without javascript?

I see in Stack overflow the search is ?q= but they don't have a submit button.

like image 458
Moak Avatar asked Nov 30 '10 06:11

Moak


People also ask

How do I stop a form from submitting a button?

preventDefault() to stop the default form submit behavior from running.

How Stop form submit on click of submit button?

We use the preventDefault() method with this event to prevent the default action of the form, that is prevent the form from submitting. Example: HTML.

Can I submit form with GET method?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ).

How do I stop a form from refreshing after submitting?

Use the preventDefault() method on the event object to prevent a page refresh on form submit in React, e.g. event. preventDefault() . The preventDefault method prevents the browser from issuing the default action which in the case of a form submission is to refresh the page.


2 Answers

You can omit name attribute in the final input like this:

<form action="/search" method="get">
    <input type="text" name="q" value="">
    <input type="submit" value="search">
</form>

Should do the trick. Keeping value attribute allows you to manipulate what text is displayed on the button.

like image 59
Michal M Avatar answered Jun 13 '23 05:06

Michal M


For the record, you can also omit the submit button if you like, and the form will submit when you press return after typing your search term. (This is how the Stack Overflow search box works).

like image 35
Nick F Avatar answered Jun 13 '23 05:06

Nick F