Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Count radio buttons

New Here ,
Nov 28, 2007 Nov 28, 2007
If I have a form, is it possible to count how many radio buttons are checked? For example I have a form with 100 yes and no questions, at the end of the form is it possible to count how many yes and nos are checked? And how do I code for that?


Thanks,
Lenny
TOPICS
Server side applications
539
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 28, 2007 Nov 28, 2007
Lenny@ford wrote:
> If I have a form, is it possible to count how many radio buttons are checked?
> For example I have a form with 100 yes and no questions, at the end of the form
> is it possible to count how many yes and nos are checked? And how do I code for
> that?

When asking this sort of question, it helps if you say which server
technology you want to use. However, the principle is the same in all of
them. Give each radio button group the same name ending in an
incremental number, for example q1, q2, q3, etc. Then loop through the
contents of the POST data to find which ones have a value of yes.

In PHP:

if ($_POST) {
$yes = 0;
for ($i = 1; $i <= 100; $i++) {
if (isset($_POST['q'.$i]) && $_POST['q'.$i] == "yes") {
$yes += 1;
}
}
}

$yes contains the number of "yes" answers. 100 - $yes is the number of
"no" answers.
--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 28, 2007 Nov 28, 2007
Lenny@ford wrote:

> If I have a form, is it possible to count how many radio buttons are checked?
> For example I have a form with 100 yes and no questions, at the end of the form
> is it possible to count how many yes and nos are checked? And how do I code for
> that?
>
<script type="text/javascript">
howMany=function(){
var inputs=document.getElementsByTagName("input"),j=inputs.length,ckd=0;
while(j--){if(inputs.type=="radio" && inputs.checked) ckd++ }
alert(ckd)}
</script>

<input name="a" type="radio" value="a1" checked>
<input name="a" type="radio" value="a2" >
<input name="c" type="radio" value="c1" >
<input name="c" type="radio" value="c2" >
<input name="c" type="radio" value="c3" >
<input name="c" type="radio" value="c4" >

<input name="b" type="button" onclick="howMany()" value="How Many">

Mick
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 28, 2007 Nov 28, 2007
On Wed, 28 Nov 2007 13:29:34 +0000, David Powers <david@example.com>
wrote:

>if ($_POST) {
> $yes = 0;
> for ($i = 1; $i <= 100; $i++) {
> if (isset($_POST['q'.$i]) && $_POST['q'.$i] == "yes") {
> $yes += 1;
> }
> }
>}
>
>$yes contains the number of "yes" answers. 100 - $yes is the number of
>"no" answers.

Not necessarily. If there are any unanswered questions, there may be
less "no" answers. I'd suggest:

if ($_POST) {
$yes = 0;
$no = 0;
for ($i = 1; $i <= 100; $i++) {
if (isset($_POST['q'.$i])) {
if ($_POST['q'.$i] == "yes") {
$yes ++;
} else {
$no ++;
}
}
}
}

Now, $yes contains the number of "yes" answers, $no contains the number
of "no" answers. The number of questions answered is the sum of $yes and
$no.

Gary
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 28, 2007 Nov 28, 2007
Gary White wrote:

> On Wed, 28 Nov 2007 13:29:34 +0000, David Powers <david@example.com>
> wrote:
>
>
>>if ($_POST) {
>> $yes = 0;
>> for ($i = 1; $i <= 100; $i++) {
>> if (isset($_POST['q'.$i]) && $_POST['q'.$i] == "yes") {
>> $yes += 1;
>> }
>> }
>>}
>>
>>$yes contains the number of "yes" answers. 100 - $yes is the number of
>>"no" answers.
>
>
> Not necessarily. If there are any unanswered questions, there may be
> less "no" answers. I'd suggest:
>
> if ($_POST) {
> $yes = 0;
> $no = 0;
> for ($i = 1; $i <= 100; $i++) {
> if (isset($_POST['q'.$i])) {
> if ($_POST['q'.$i] == "yes") {
> $yes ++;
> } else {
> $no ++;
> }
> }
> }
> }
>
> Now, $yes contains the number of "yes" answers, $no contains the number
> of "no" answers. The number of questions answered is the sum of $yes and
> $no.
>
> Gary

http://www.w3.org/TR/html401/interact/forms.html#radio

If no radio button in a set sharing the same control name is initially
"on", user agent behavior for choosing which control is initially "on"
is undefined. Note. Since existing implementations handle this case
differently, the current specification differs from RFC 1866 ([RFC1866]
section 8.1.2.4), which states:
At all times, exactly one of the radio buttons in a set is checked. If
none of the <INPUT> elements of a set of radio buttons specifies
`CHECKED', then the user agent must check the first radio button of the
set initially.

Since user agent behavior differs, authors should ensure that in each
set of radio buttons that one is initially "on".

Mick
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 28, 2007 Nov 28, 2007
.oO(David Powers)

>In PHP:
>
>if ($_POST) {
> $yes = 0;
> for ($i = 1; $i <= 100; $i++) {
> if (isset($_POST['q'.$i]) && $_POST['q'.$i] == "yes") {
> $yes += 1;
> }
> }
>}

Another idea:

You could use array syntax for the names of the radion button groups,
e.g. q[1], q[2], q[3] etc. Then something like this should be enough:

if (isset($_POST['q']) && is_array($_POST['q'])) {
$result = array_count_values($_POST['q']);
}

Micha
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 28, 2007 Nov 28, 2007
LATEST
On Wed, 28 Nov 2007 11:23:35 -0500, Mick White
<himselfBOGUS@mickweb.com> wrote:

>Since user agent behavior differs, authors should ensure that in each
>set of radio buttons that one is initially "on".

That's true. However, many people don't follow that guideline and start
out with none checked. Also, the Firefox developers toolbar has an
option to clear radio buttons. I think it's safer to test them.

Gary
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines