Re: parsing select multiple="multiple"
| From: | John Taylor-Johnston | Date: | Tue, 19 Feb 2013 19:02:21 +0000 |
| Subject: | Re: parsing select multiple="multiple" | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-320245@lists.php.net to get a copy of this message | ||
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? ---------------------------------------------- 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? ---------------------------------------------- Question 3: My code works, perfectly. 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']) 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";
}
}
?>