10

In HTML5, an input without name is also valid

e.g.

      <input type="text" id="user"  />

But what is the point if I cannot access the form element in my backend PHP code?

Howard
  • 19,215
  • 35
  • 112
  • 184

2 Answers2

15

Not all input is used server-side. In many cases it's used for form submission via AJAX. Additionally, a JavaScript app can make use of user input without ever needing to use a form.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • In general, I still use a `name` for posting with AJAX. Otherwise you have to construct the data manually, on the server end you will still need a name/index. – Wesley Murch Aug 02 '11 at 17:43
  • @Wesley, I try to avoid data repetition. I would reuse the `id` in cases where it's applicable (radio buttons are a notable exception). However, I don't disagree that the `[name]` is a good way to store dynamic data for AJAX submission. – zzzzBov Aug 02 '11 at 17:57
  • I rarely use ids, so it's just a style difference I guess. I can't think of any other practical cases for an input with no name... Either it's strictly javascript related or used for copy/pasting. Can you think of another? – Wesley Murch Aug 02 '11 at 18:06
  • @Wesley, You can do pure css toggleable lists and accordions using `input:checkbox`, or `input:radio` using the `:checked` and sibling selectors. It's not *too* difficult to do, but I don't bother because I try to use HTML CSS & JS in an MVC structure. Interactions belong in JS. – zzzzBov Aug 02 '11 at 18:17
  • No that's a great point, you can abuse `:focus` to trigger changes with CSS using the adjacent selector `+`. Like so: http://jsfiddle.net/HWFPz/1/ EDIT: Yes, exactly :) – Wesley Murch Aug 02 '11 at 18:22
4

Click the "link" button on any question or answer here on Stack Overflow, you will see an example of an <input> without a name or associated form.

Granted, this particular input is created with javascript - but it's pretty common to see an input field or textarea for copy/paste purposes, for one example.

..and it's also useful for basically anything to do with javascript.

One non-AJAX example I am currently using:

I have a spreadsheet for several dollar amounts to be filled in. I use an <input> field with no name to display the total amount of each column with javascript. On the server side, I don't need a "total amount" field coming through, and I sure as hell wouldn't trust it. The real total amount is calculated on the server side based on the other inputs, but we still show it in real time on the front end for the user.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • 1
    Just realized that my second example would be better of with a non-input element like a ``, but that was part of the client's specific request. Oh well... – Wesley Murch Aug 02 '11 at 23:51
  • 1
    The example is still valid, I think - you might well want an `` so that (for example) the user can easily select and copy the values, which might be less convenient with a ``, for one because copying a `` will copy not only the value, but also the formatting, which is most likely not what you want. – mindplay.dk Sep 15 '14 at 09:51