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>
2
  • Here you go stackoverflow.com/questions/4709926/… Commented Jul 2, 2013 at 4:40
  • 2
    There is nothing special about "[]" in element names to a browser (note that the specification 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. Commented Jul 2, 2013 at 4:41

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.