Howard
Howard

Reputation: 19825

What is the point of input without name in HTML5?

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?

Upvotes: 10

Views: 5981

Answers (3)

Fran&#231;ois J.
Fran&#231;ois J.

Reputation: 71

I am implementing CSP (Content Security Policy) in a PHP app.

This implies moving all inline JavaScript to external files.

Here is an example of hidden input use, without a name:

?>
<input type="hidden" id="my_var" value="<?php echo htmlspecialchars( $my_var ); ?>" />
<script src="my_script.js"></script>
<?php
// my_script.js
var myVar = document.getElementById('my_var').value;

This is the best way I come up with to transfer PHP variables to JS without using inline scripts.

And of course, I wouldn't want/need those hidden inputs to be submitted if they happen to be inside a form.

Upvotes: 0

No Results Found
No Results Found

Reputation: 102874

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.

Upvotes: 4

zzzzBov
zzzzBov

Reputation: 179264

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.

Upvotes: 16

Related Questions