Re: parsing select multiple="multiple"
| From: | tamouse mailing lists | Date: | Thu, 21 Feb 2013 06:45:00 +0000 |
| Subject: | Re: parsing select multiple="multiple" | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-320275@lists.php.net to get a copy of this message | ||
On Tue, Feb 19, 2013 at 1:02 PM, John Taylor-Johnston
<John.Taylor-Johnston@cegepsherbrooke.qc.ca> wrote:
>
> tamouse mailing lists wrote:
>>>
>>> >I hate arrays. :D
>>
>> Here's a small snippet showing how it works, I hope:
>>
>> foreach ($DPRpriority as $item => $value) {
>> echo "<li> ".$item.": ".$value['name']."
>> selected:
>> ".$value['selected']." </li>\n";
>> }
>>
> Question 1: when did we have to add [] to a <input> name to turn it into an
> array?
>
> <input type="checkbox" name="DPRlocationdetails[]"
> value="Unknown">
>
> According to phpinfo() it still comes out as $_POST['DPRlocationdetails']
> without [].
>
> Are the [] necessary?
[] are necessary when you want to return multiple values for a form
field, or collection of form fields such as checkbox. AFAIK, this has
always been the case with PHP. See
https://gist.github.com/tamouse/5002728
> ----------------------------------------------
> Question 2:
> I was looking at some code in the Manual, where someone used isset and
> is_array.
>
> How necessary is if(isset($_POST['DPRlocationdetails']))
>
> and then to use: if(is_array($_POST['DPRlocationdetails']))
>
> That seems like over kill?
It's more defensive, in the case where someone may be by-passing your
form to send things in.
> ----------------------------------------------
> Question 3:
>
> My code works, perfectly.
Then there must be no questions. :)
> In this case, I decided to attack some check-boxes
> first. The resulting function will work for <select multiple> too..
>
> Does anyone see me doing something wrong in my code below?
>
> My questions are:
>
> Is this the only way to pass "Unknown", "Family Home" or
> "Apartment" into
> the function?
>
> Is this correct?
>
> ---- if ($_POST['DPRlocationdetails'] == "Unknown")
>
> Somebody once told me I had to do it this way?
>
> ---- if ("Unknown" == $_POST['DPRlocationdetails'])
In this scenario, these are equivalent. There is no preference of one
over the other.
I *have* heard claims that something like this is preferrable, though:
if (FALSE === $variable)
But I think that may have been due to some misunderstanding of
precedences in the following sort of scenario:
if (FALSE === ($result = some_function())
where if done this way:
if ($result = some_function() === FALSE)
was giving them bad results.
> John
>
> --------------------snip---------------------------
>
> <form action="foo.php" id="DPRform" method="post"><input
> value="Update"
> type="submit">
> <input type="checkbox" name="DPRlocationdetails[]"
> value="Unknown" <?php
> filter_value($_POST['DPRlocationdetails'],"Unknown"); ?>> Unknown
> <input type="checkbox" name="DPRlocationdetails[]" value="Family
> Home" <?php
> filter_value($_POST['DPRlocationdetails'],"Family Home"); ?>> Family
> Home
> <input type="checkbox" name="DPRlocationdetails[]"
> value="Apartment" <?php
> filter_value($_POST['DPRlocationdetails'],"Apartment"); ?>> Apartment
> </form>
>
> <?php
> function filter_value($tofilter,$tofind) {
> foreach($tofilter as $value){
> if ($value == $tofind) echo "checked";
> }
> }
> ?>
>
>