Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button vs link vs input type="submit" on a form

A user logs in to his control panel and sees his incoming messages. Near each message there is a "Reply" button. What is the correct way to implement that button?

I see three main options:

  1. Use a link:

    <a href="customer.php?reply&messageid=1234">Reply</a>.
    

    Disadvantage:

    • We need to style that link to look like a button. Because I think that action "Reply" should be represented by a button, not a link (in my opinion links should be used when they link to some resource and when we have a noun in link text; and if we want to make an action and have a verb (action) in a caption - button should be used).

  1. Use a button:

    <button onclick="location.href='customer.php?reply&messageid=1234'">Reply</button>`  
    

    Disadvantage:

    • The user must have JavaScript enabled. Though based on our statistics 99.8% of our users have JavaScript enabled, and if they don't it will be really difficult for them to work on our website anyway (we have many features implemented with JavaScript). So I think that 100% of our actual active users have JavaScript enabled.

  1. Use a form with <input type="submit">:

    <form action="customer.php?reply" method="get">
      <input name="messageid" type="hidden" value="1234" />
      <input type="submit" value="Reply" />
    </form>
    

    Disadvantage:

    • I think using form here is "artificial". A user doesn't enter anything. We use the form just to make our button work. I also think that using POST request when we don't change anything and just need to show a reply form to a user - violates REST principles. But even with using GET I still think that using form is artificial in this case.

Some other notes:

  • Using a button inside a link doesn't work in IE.
  • It's a private section of our website so search engines can't see it and we don't really need a link to help search engine follow it and index the resource (it's a usual argument for using links in web instead of buttons)

Which one would you choose and why?

UPD. Well, I have decided to use a link. Thank you everyone for discussion!

like image 876
nightcoder Avatar asked Jul 12 '12 22:07

nightcoder


People also ask

Should I use input or button for Submit?

input suggests that the control is editable, or can be edited by the user; button is far more explicit in terms of the purpose it serves. Easier to style in CSS; as mentioned above, FIrefox and IE have quirks in which input[type="submit"] do not display correctly in some cases.

Does input type button submit a form?

Definition and Usage. The <input type="submit"> defines a submit button which submits all form values to a form-handler. The form-handler is typically a server page with a script for processing the input data. The form-handler is specified in the form's action attribute.

What is the difference between submit button and normal button in a form?

It depends on your purpose. In your code, if you want to submit the form to your server, you should use "Submit". However, if you want to do something from client side by clicking the "Button", the input type "Button" is the choice.

What is the difference between button and input type button?

The <button> tag permits phrasing content inside button element contents like text or images etc, along work with type functionality defined. But the input type=”button” attribute does not permit content.


4 Answers

I would definitely use a link: progressive enhancement.

You want the button to be usable even with Javascript turned off (remember: every user is a non-javascript-user for the duration of the page load. If they're on a slow connection (e.g. mobile), they should be able to reply as soon as they see the button).

Styling is a non issue (you weren't gonna use the default button styles, were you?).

Using POST when the user isn't submitting anything sure is wrong. Even with GET, it's still not really form material...

like image 185
Joseph Silber Avatar answered Sep 21 '22 06:09

Joseph Silber


It's pretty easy to style <a> and <button> identically, just use a common class name. <input type="button"> can be a little trickier, but you don't need to use it.

Your tag choice should never be dictated by your intended presentation, but what the element is and what it does. Links should be marked up as <a>.

like image 25
Wesley Murch Avatar answered Sep 19 '22 06:09

Wesley Murch


I agree that a POST is wrong. So, set your form to use method="get". Use just one form and leave out the hidden fields. Using <button>, the displayed text can differ from the submitted value.

<form action="customer.php" method="get">
    <input type="hidden" name="reply" />
    <div class="message">
        <div class="messageBody">..</div>
        <button name="messageid" value="1234">Reply</button>
    </div>
    <div class="message">
        <div class="messageBody">..</div>
        <button name="messageid" value="1235">Reply</button>
    </div>
    ...
</form>
like image 34
gilly3 Avatar answered Sep 21 '22 06:09

gilly3


All methods are correct, except that method 2 is correct only under the assumption that you can safely ignore non-JavaScript browsing.

The assumptions and comments presented about forms are incorrect, or at least misleading. A form need not involve user input; forms can be used e.g. to submit previously collected data, with no other fields but a submit field. And the POST method can be used even when not changing anything, e.g. due to the amount of input data (as there are fairly low upper limits on GET data); besides, the form presented in the question uses GET, the default method.

Otherwise, this is mostly a non-constructive question, calling for discussion and argumentation rather than technical solutions.

like image 38
Jukka K. Korpela Avatar answered Sep 18 '22 06:09

Jukka K. Korpela