156

I'd like to create an HTML form submit button with the value 'add tag', however, the web page is in Swedish, so I'd like to have a different button text.

That is, I want to have a button like

enter image description here

but I want to have my code like

if (request.getParameter(cmd).equals("add tag"))
    tags.addTag( /*...*/ );

Is this possible? If so, how?

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 4
    I think the value should not matter. You should simply check for the presence of "add_tag" in your POST data – greg0ire Nov 13 '10 at 08:51
  • 7
    I read this as "I want the text of the input not to be relevant to my server side processing" so that when you have several translations for a form you don't get one of several text values posted. It matters when your form has several submit buttons and you want to do if(request.getParameter("submit-type").equals("add-tag") ... to see which one was pressed. – ijw Nov 13 '10 at 09:03

5 Answers5

253

It's possible using the button element.

<button name="name" value="value" type="submit">Sök</button>

From the W3C page on button:

Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities: the BUTTON element may have content.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 7
    Just a note, this doesn't work as advertised on IE6/7. This article may correct the problem, but you'll have problems if you're using asp.net or another framework that disallows HTML on postback: http://allinthehead.com/retro/330/coping-with-internet-explorers-mishandling-of-buttons – ristonj Jan 27 '12 at 17:52
  • you could also just use the name property which provides output in POST – TheSatinKnight Apr 14 '20 at 21:44
25

Following the @greg0ire suggestion in comments:

<input type="submit" name="add_tag" value="Lägg till tag" />

In your server side, you'll do something like:

if (request.getParameter("add_tag") != null)
    tags.addTag( /*...*/ );

(Since I don't know that language (java?), there may be syntax errors.)

I would prefer the <button> solution, but it doesn't work as expected on IE < 9.

Sony Santos
  • 5,435
  • 30
  • 41
15

There are plenty of answers here explaining what you could do (I use the different field name one) but the simple (and as-yet unstated) answer to your question is 'no' - you can't have a different text and value using just HTML.

ijw
  • 4,376
  • 3
  • 25
  • 29
2

I don't know if I got you right, but, as I understand, you could use an additional hidden field with the value "add tag" and let the button have the desired text.

Flinsch
  • 4,296
  • 1
  • 20
  • 29
  • 4
    Not helpful with multiple buttons on the same form, which I think is the OP's problem. – ijw Nov 13 '10 at 08:59
  • Cannot see any evidence from his question that there are multiple buttons on the same form. – Darren Feb 07 '13 at 00:08
0

If you handle "adding tag" via JScript:

<form ...>
<button onclick="...">any text you want</button>
</form>

Or above if handle via page reload

Andrey Pokhilko
  • 2,578
  • 18
  • 19
  • 2
    Yay, JS required for every single form, regardless of simplicity. Welcome to ASP.NET. – ijw Nov 13 '10 at 08:52
  • As an ASP.Net developer, I can confirm this. :P Assuming you're still living in the days of webforms anyway. ASP.Net MVC is a lot better. No viewstates, globally wrapping forms, etc. – James Billingham Jan 31 '13 at 23:01