0

Basically I want to give the user the ability to enter a bunch of attributes, So, as the user selects the last input in a list we will create another one.

That's real easy, now the question comes to naming and fetching on the server, I know in google chrome the below code is valid and will give me an array on the other end. But I cannot seem to find this information anywhere regarding browser compatibility with this feature, do all browsers support this? or is is only truly valid for selects for example.

If someone could explain, or point me towards the correct W3C guidlines for this it would be great.

<!doctype html>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

    <?php if(!empty($_POST)){
        xdebug_var_dump($_POST);
    } ?>

    <form method="post">
        <input name="testing[]" value="testing1" />
        <input name="testing[]" value="testing2" />
        <input name="testing[]" value="testing3" />
        <input name="testing[]" value="testing4" />
        <input name="testing[]" value="testing5" />
        <button type="submit">Go</button>
    </form>
</body>
</html>
Hailwood
  • 89,623
  • 107
  • 270
  • 423
  • Here you go http://stackoverflow.com/questions/4709926/how-can-i-use-the-array-format-input-field-names-in-my-html-form-that-posts-to-p – Yogesh Suthar Jul 02 '13 at 04:40
  • 2
    There is nothing special about "[]" in element names to a browser (note that [the specification](http://www.w3.org/TR/REC-html40/interact/forms.html#control-name) does not mention "[]" at all here). *All element pairs* are transmitted (the wording is different, but it's about like this); so, what's it for? Well, it's for the server to "understand" that it's a collection. – user2246674 Jul 02 '13 at 04:41

3 Answers3

2

The name attribute for an input is very flexible - you can include almost any character, although you probably don't want to explore too many options. When a form like this is submitted the data is submitted as you see it, all with the same name as you'd expect.

On the server side, however, things can be different. In this case PHP specifically will interpret a name such as testing[] as an array element, and will combine all input fields with such a name into a single array. It's a great way to pass variable length data into a PHP script.

this is not so much about HTML standards as PHP being helpful.

2

There is no browser compatibility issues with it. It works in all browsers. You can go ahead with this code.

Vishnu Sureshkumar
  • 2,246
  • 6
  • 35
  • 52
0

From what I have read so far, it is not anything related to browser compatibility. The browser still sends the data as is, PHP is the one that interprets the $_POST data as such:

A link on PHP.net describing this

Another slightly similar question on Stack Overflow

Use this example at PHPFiddle on some different browsers to show that it works.

Community
  • 1
  • 1
SomeShinyObject
  • 7,581
  • 6
  • 39
  • 59