How to get all elements of a dropdown list into a variable o

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hmdnawaz
Forum Newbie
Posts: 14
Joined: Tue Dec 21, 2010 10:55 pm

How to get all elements of a dropdown list into a variable o

Post by hmdnawaz »

I have a dropdown list which have some elements.
I want to get all the elements of the dropdown list into a variable of type array.

Any Idea???
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to get all elements of a dropdown list into a variab

Post by social_experiment »

The square brackets indicate that the values from the select element will be available as an array

Code: Select all

<select name="listOfItems[]">
<option value="1">One</option>
<option value="2">Two</option>
</select>
To access the values

Code: Select all

<?php
 if (is_array($_POST['listOfItems'])) {
  foreach ($_POST['listOfItems'] as $value) {
   echo $value;
  }
}
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply