Forum Moderators: coopster

Message Too Old, No Replies

When to use array syntax in form elements

interest question

         

jatar_k

6:26 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<select name="categories[]">

Just for my own interest, why would you want the whole array of choices? Aren't you concerned with the selections only?

I have never used the array style and never found a need so it always perplexes me as to why people need it at all. I need some examples.

come on, come all, I'm intrigued.

DrDoc

7:25 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP creates the array for you... with one element for each selection :)

Here's a page that describes a simple scenario:
[perl.about.com...]

jatar_k

7:47 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I saw that link in the other thread ;)

I understand how it works. I am asking what the purpose is, why do it? What possible advantage is there to it?

I guess I have seen a lot of people putting the array styled names and for most of the scenarios it makes no sense.

multiple uploads, sure
multiple choice elements, I guess, I very seldom find a need for those.

any other form elements where it would be necessary?

dreamcatcher

8:02 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



any other form elements where it would be necessary?

I think some people use it in Tell a Friend scripts, where you can specify more than one recipient. I have never found a need for it myself as yet with the exception of multiple file uploads as jatar_k has already mentioned!

coopster

8:13 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<select name="categories[]">

Just for my own interest, why would you want the whole array of choices? Aren't you concerned with the selections only?

I want to clear one thing up before it gets misunderstood. This will not return the whole array of choices. You must add the multiple attribute:


<select name="names[]" multiple="multiple">

Without multiple, you won't be able to select more than one which is a requirement in order to return more than one. But I'm sure you already know that ;) And if you do indeed want more than one selection returned you must use the double brackets[] otherwise PHP will only return one element (usually the last one).

Applications? We use multiple choice elements quite often.
One example might be listing a group of people that the user can select one or more from the list and send an email to them.

To the best of my knowledge, using the brackets is the only way to get multiple selections back from a form element (<select> and <checkbox> especially).

coopster

8:23 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Here's a quick snippet to play around with.

<html><head><title>PHP HTML Arrays</title></head><body>
<h1>PHP HTML Arrays</h1>
<form method="post" action="select.php">
<select name="names[]" multiple="multiple">
<option value="Daffy Duck">Daffy Duck</option>
<option value="Bugs Bunny">Bugs Bunny</option>
<option value="Elmer Fudd">Elmer Fudd</option>
<option value="Sylvester">Sylvester</option>
<option value="Tweety Bird">Tweety Bird</option>
</select>
<input type="submit" name="Submit" value="Submit" />
</form></body></html>
<?php
if ($_POST['Submit']) {
print '<pre>'; print_r($_POST['names']); print('<pre>');
}
?>

jatar_k

9:24 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



slick little snippet, saves me the time to do one ;)

I have seen various examples of using the array syntax on many types of elements. In this case it makes tons of sense. It just seems that in a bunch of things I have read recently (posts, tutorials etc..) they use it a lot and it seems pointless.

Just seems to be something rather new, I had only seen it mentioned in the proper place before not in every tutorial.

thanks for clarifying what I was saying coopster, I wasn't being very concise (none of us are immune to confusing posts when confused :)).

Timotheos

9:31 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jatar,

I guess my question back would be 'how else could you do it?'

For example on my site I have a multiple choice quiz at the end of each section. Some of the questions are of the type that you 'pick all that apply'.

Another example I've seen is when applying for one of those free magazine subscriptions they often ask "What do you purchase? (check all the apply)" "Which of these do you use"?, etc. etc.

Tim

jatar_k

9:34 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I guess I just use checkboxes for stuff like that.

Saves having to put "hold down ctrl and click on each that applies or use shift to ....."

Standard paper form would say
"check all that apply"

so I let them check

DrDoc

9:47 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...but you use different names for the check boxes?

coopster

9:52 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You would have to use different names, otherwise you won't get all the selected values back, unless of course you use the [] syntax to create an array as being described in this thread.

jatar_k

9:57 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



yep different names, doesn't make any difference in my foreach($_POST as $key => $value) processing.

coopster

10:02 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



BTW, jatar_k, I agree with you that checkboxes and/or radio buttons are much easier to explain to the average user as opposed to "did you know you can select multiple items by holding down your shift key for a range or your ctrl key as a toggle"?

Checkboxes and buttons are much, much easier to comprehend with little or no explanation. The tough part is when that list could be, say, 25 or more entries/selections -- especially when you have a dozen or more additional <input> elements on your screen! Your user will be scrolling forever.

Side Tip:
---------
I usually build some tailored options into my lists and set a $maxlist parameter. If the number of entries I have in my list is greater than $maxlist, switch the input type from a checkbox/radio button to a select list.

Timotheos

10:06 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah I see. The question is between multiple select vs check boxes. I only use check boxes too.

stidj

11:08 pm on Nov 12, 2003 (gmt 0)

10+ Year Member



jatar_k said:

<select name="categories[]">
Just for my own interest, why would you want the whole array of choices? Aren't you concerned with the selections only?

I have never used the array style and never found a need so it always perplexes me as to why people need it at all. I need some examples.

come on, come all, I'm intrigued.

my response:

That is because I have a database of different companies. Each company may sell more than one category of items such as Hardware, Electronics, etc....... so in some cases I will need to select more than one item.

figment88

12:30 am on Nov 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you use an array for multiple checkboxes or even multiple other form types, you do not need to keep track of the names when you process.

For example, you could get the length of the array, iterate over it in a for loop, replacing all the single quotes with double ones.

Advantages are you don't need to test for missing values (e.g. if!emtpy ...), you can reuse code easier, etc.

While coding without having useful variable names can be quick, it often leads to big mistakes, and renders the code opaque if someone else needs to work on it.