Jump to content

Counting selections


Suchy

Recommended Posts

I am having trouble with counting checkboxes

 

// Form
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data" name="form1" id="form1" onSubmit="return checkform(this);" >
	 <label><input type="checkbox" name="win_ver[]" value="00">Windows 95</label><br />
	 <label><input type="checkbox" name="win_ver[]" value="01">Windows 98</label><br />
	 <label><input type="checkbox" name="win_ver[]" value="02">Windows ME</label><br />
	 <label><input type="checkbox" name="win_ver[]" value="03">Windows XP</label><br	
     	 <label><input name="OK" type="submit"   value="OK" /><label>
</form>

 

function checkform(form1) {
var total = 0;
for (i = 0; i <12; i++) 
{
if ((form1.win_ver[i].checked))
{
    	total += 1;
    }
}
alert("You selected " + total + " boxes.");
return false;
}

 

What is wrong with this ?

Link to comment
Share on other sites

This should work for you:

 

function checkform(form1) {
  var total = 0;
  for(i=0;i < form1['win_ver[]'].length;i++)
  {
    if ((form1['win_ver[]'][i].checked))
    {
      total += 1;
    }
  }
  alert("You selected " + total + " boxes.");
  return false;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.