1

Is there anything bad in using post arrays for post variables?

<input type="text" id="stuff" name="stuff[text]"/>

instead of

<input type="text" id="stuff" name="stuff"/>    

Tips when to use them?

2
  • 1
    nope, as long as it makes sense in a logical way. No restrictions. Commented Apr 21, 2012 at 19:39
  • Post arrays are one of php useful features, as John said, use them when it is logical to do so. Commented Apr 21, 2012 at 19:44

3 Answers 3

2

No, there is no reason not to do this.

However, PHP is pretty much the only language which allows to create arrays like this - so if you ever change your backend to a different language you might have to change things.

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

Comments

1

It isn't a bad way. Typically programmers use array in such manner to post array with non-determined lenght. Still. Don't know for certain, but when You want to change method to GET then on IE <= 8 is a limit to 2048 chars in address lenght. And dynamic generated array can easy depleat this limit. On other browsers limit is much higher or there is none.

Another drawback of this method is that PHP will preceed correctly, but other server side languages may not. This isn't specified in official HTML docs, as far I know.

So it is more convinient to put it in a single cell in post array, that do subarray. If You want to do some namespacing, then You can write name in such way:

name="styff.text"

as do some of forum engines (for certain Vanilla 2 does).

If it has no diffrence to You I would stay to single variable name in html names. Mostly because of backend.

For tips how to use them I could recommend to use such array to cover dynamic generated content on site. Still it can be handled with normal names, but it is pretty ugly. If we have a case that You want to do a picture adding system, that I would name each input file with "pic[]" and on server iterate whole table. The same thing for generating documents on client side. I would then do names like "content[][name]" "content[][type]" "content[][value]" and so on. Whathever I woud have in document part class I would throw in this kind of naming, and on server just check is set and do certain things for certain block of document.

This could be talked for a long time since every programmer have own technics and they tend to stick with it. For example I throw a in every form I have on site, and each action is parsed by a generall controller, and then passed by do specific controllers.

Comments

0

Nothing bad. If it is useful four you, use it.

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.