Question

What the difference between 2 name of input ?

OK, Normal i usually use this format name="xxxxxx"

<input type="text" name="xxxxxx"/>

But, today i see name format that i not understand name="xxxxx[]"

<input type="text" name="xxxxxx[]"/>

what is [] in name="xxxxx[]"

Was it helpful?

Solution 2

In HTML5, the name attribute is just a string (without any special syntax). The only thing that can have a special meaning are the _charset_ and isindex strings. Thus square brackets themselves are nothing special.

However, the authors of programming languages or libraries that interact with HTML forms some times decide to define special syntaxes. That's the case of the PHP server-side language, where paired brackets in form element names are used by the language to automatically define variables of array type. See How do I create arrays in a HTML ? for further details.

(It's possible that other langs make use of similar conventions but I don't really know.)

OTHER TIPS

With this format xxxxx[] the variable $_POST['xxxxx'] is an Array when form is posted. For example, is possible to iterate by the $_POST['xxxxx']:

<?php 
$data = filter_input(INPUT_POST, 'xxxxx');

if(is_array($data)) {
    foreach($data as $value) {
        echo $value;
    }
}
?>

Nothing, it just another name, may be something auto generated or someone use for a purpose in mind.

This is mainly done because of server side frameworks.

With PHP, for instance, if you had

<input type="text" name="address[firstline]">

and

<input type="text" name="address[secondline]">

and submitted the form, in your PHP code on the server you'd retrieve a single address object from the request and it would have the keys firstline and secondline on it.

you can still query using jQuery:

$('input[address\\[\\]=firstline]')

The reason for needing two backslashes is because a single backslash is interpreted as a JavaScript string escape character, so you need two to specify a literal backslash, which provides the escape character to the selector...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top