2

I've seen people doing this:

<input type="text" name="myText[]">

What is the purpose of the []? Is it supposed to autoindex so you get an array or list at the server when the form is submitted?

Thanks.

2 Answers 2

1

The purpose is to simulate an array. If you have 10 inputs named images[], you can access their values (in PHP) like this :

foreach ($_POST['images'] as $img)
{
 // do something
}
Sign up to request clarification or add additional context in comments.

4 Comments

That's what I thought, but is this a PHP only thing? I am using a Python server and when I loop over request.params.items(), using your example, I get images[], images[], images[] ...
Looks like it's not implemented in Pyramid (my server). stackoverflow.com/questions/6734702/…
I believe it's a general behaviour
for anyone trying to do this with Python Pyramid, checkout the Peppercorn module.. its very cool
1

Yes, Its used to create an array... so you can have multiple inputs with the same name and processes them in php as an array. If you want to access the individual elements in jquery/javascript then give each of them a unique ID and/or class.

1 Comment

It validates using the firefox extension. HTML just treats it as a name. PHP interprets it as an array. The javascript engine doesn't know how to interpret the special character. @MFB not sure about Python.

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.