count multi select combo's selection

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
soclose
Forum Newbie
Posts: 19
Joined: Tue Nov 10, 2009 12:43 am

count multi select combo's selection

Post by soclose »

Hi All,

I'd like to count the no. of selection of multi select combo and describe this count no. into a text box dynamically. how to code?
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: count multi select combo's selection

Post by pbs »

You need to give name for select box as an array like "selectbox[]". Now when you submit form you will get its count by "count($_POST['selectbox'])".
soclose
Forum Newbie
Posts: 19
Joined: Tue Nov 10, 2009 12:43 am

Re: count multi select combo's selection

Post by soclose »

Thanks, Pbs. The counted no. will be dynamically described clicking or selecting the multi-combo's items, not form submitting. Please give me a sample code.
soclose
Forum Newbie
Posts: 19
Joined: Tue Nov 10, 2009 12:43 am

Re: count multi select combo's selection

Post by soclose »

Hi All,

my web form design has only two controls: a multi combo box and a text box. When user selects the items of this multi combo, its selected count will be described in the text box dynamically. how to code?
soclose
Forum Newbie
Posts: 19
Joined: Tue Nov 10, 2009 12:43 am

Re: count multi select combo's selection

Post by soclose »

my code is below:

<td>
<select name="options[]" size="4" multiple >
<option value="magnifier">Magnifier</option>
<option value="reducer">Reducer</option>
<option value="timer">Timer</option>
<option value="oscillator">Oscillator</option>
</select>
<td>
<?php
echo count($_POST['options']);
?>

But this does not work. :(
tang
Forum Newbie
Posts: 6
Joined: Sun Nov 15, 2009 9:14 pm

Re: count multi select combo's selection

Post by tang »

<select name="ok" id="ok" size="4" multiple onChange="selvalue()" >
<option value="magnifier">Magnifier</option>
<option value="reducer">Reducer</option>
<option value="timer">Timer</option>
<option value="oscillator">Oscillator</option>
</select>
<input name="text" type="text" id="text" value="skdjfksd">
<script>
function selvalue(){
text.value=ok.options[ok.selectedIndex].text
}
</script>




this code can do it, hope can help you! Don't use php code do it. you may do it in "javascript", it's easy!
soclose
Forum Newbie
Posts: 19
Joined: Tue Nov 10, 2009 12:43 am

Re: count multi select combo's selection

Post by soclose »

Thanks All. I get it! :lol:

<select name="ok[]" id="ok" size="4" multiple onChange="valList(this);">
<option value="magnifier">Magnifier</option>
<option value="reducer">Reducer</option>
<option value="timer">Timer</option>
<option value="oscillator">Oscillator</option>
</select>
<input name="text" type="text" id="text" value="skdjfksd">
<script>
function valList(thissel){
var selCount = 0;
for (var i=0; i<thissel.length; i++) {
if (thissel.selected) {
selCount += 1;
}
}
text.value= selCount;
}
</script>
Post Reply