Page 1 of 1

count multi select combo's selection

Posted: Wed Dec 23, 2009 10:54 pm
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?

Re: count multi select combo's selection

Posted: Wed Dec 23, 2009 11:09 pm
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'])".

Re: count multi select combo's selection

Posted: Fri Dec 25, 2009 8:00 pm
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.

Re: count multi select combo's selection

Posted: Tue Dec 29, 2009 12:07 am
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?

Re: count multi select combo's selection

Posted: Tue Dec 29, 2009 1:09 am
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. :(

Re: count multi select combo's selection

Posted: Tue Dec 29, 2009 2:49 am
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!

Re: count multi select combo's selection

Posted: Tue Dec 29, 2009 8:17 pm
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>