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
https://forums.phpfreaks.com/topic/100004-counting-selections/
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
https://forums.phpfreaks.com/topic/100004-counting-selections/#findComment-511386
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.