Forum Moderators: coopster
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.
Here's a page that describes a simple scenario:
[perl.about.com...]
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?
<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).
<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>');
}
?>
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 :)).
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
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.
<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.
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.