php counting selected items

I have a <select name=abc> tag in which a user can select multiple <options>.

Is there a way to get a count of how many <options> were selected?

Someone suggested:

count($_POST["abc"]);  This doesn't work... only shows "1".
Sheldon LivingstonConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Peos JohnPHPCommented:
Hello,

1) Hope sizeof(array,mode) function may work for this.

mode :

0 - Default. Does not count all elements of multidimensional arrays
1 - Counts the array recursively (counts all the elements of multidimensional arrays)

2) Another option would be to loop the array elements and define $i=0 and $i=$i++ inside the loop to count.

Hope this helps.
Julian HansenCommented:
Two options
1. Create your select like this (note the name)
      <select multiple name="abc[]">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
        <option value="4">Four</option>
        <option value="5">Five</option>
      </select>

Open in new window

Sample here
2. Read the raw PHP input like this
<?php $postdata = file_get_contents("php://input"); ?> 

Open in new window

Which will return a string like this
abc=1&abc=3&abc=5

Open in new window

You can get the values by doing some explode functions on it - 1 is easier
Ramesh JangraCommented:
First append '[]' with your 'name' attribute in select statement, which should look like <select multiple name='abc[]'>.
In php use-
count($_POST['abc']);
 this will return the number of elements selected.
to get the value of selected elements use below statement in php:
foreach($_POST['abc'] as $selected)
// do here whatever you want to do with selected values

Thanks
Ryan

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Ray PaseurCommented:
Here is how I might do it.  You can see the URL change, representing the request variables.  If your request mutates anything on the server, you will want to use a POST request instead of GET, but using GET enables us to see the work products more readily.
http://iconoun.com/demo/temp_classnet.php
<?php // demo/temp_classnet.php
/**
 * http://www.experts-exchange.com/questions/28848659/php-counting-selected-items.html
 */
error_reporting(E_ALL);


// IF THE FORM WAS SUBMITTED, GET THE COUNT AND SELECTED OPTIONS
if (!empty($_GET))
{
    $num = count($_GET['abc']);
    echo PHP_EOL . "THERE WERE $num SELECTIONS FOR 'abc'";
    echo PHP_EOL;
    print_r($_GET);
}

// CREATE THE HTML FORM USING HEREDOC NOTATION
$html = <<<EOD
<form>
<select multiple name="abc[]">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
    <option value="5">Five</option>
</select>
<input type="submit" />
</form>
EOD;

// WRITE THE FORM TO THE BROWSER
echo $html;

Open in new window

Sheldon LivingstonConsultantAuthor Commented:
This worked great!
Julian HansenCommented:
@classnet - please see this comment http://www.experts-exchange.com/questions/28848659/php-counting-selected-items.html#a41260846 which gave the correct answer before the selected answer.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.