Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking multiple sets of checkboxes

Status
Not open for further replies.

sophielois1

Technical User
May 2, 2006
16
GB
Hi,

Im trying to find out how to check a number of different sets of check boxes:

Code:
<form action="submit.php" method="post" onsubmit="return check(this)">

<input type="checkbox" name="knowledge1[]" value="A" /> 
<input type="checkbox" name="knowledge1[]" value="B" /> 
<input type="checkbox" name="knowledge1[]" value="C" /> 
<input type="checkbox" name="knowledge1[]" value="D" />
<br>
<input type="checkbox" name="knowledge2[]" value="A" /> 
<input type="checkbox" name="knowledge2[]" value="B" /> 
<input type="checkbox" name="knowledge3[]" value="C" /> 
<input type="checkbox" name="knowledge4[]" value="D" /> 
<br>
<input type="checkbox" name="knowledge5[]" value="A" /> 
<input type="checkbox" name="knowledge5[]" value="B" /> 
<input type="checkbox" name="knowledge5[]" value="C" /> 
<input type="checkbox" name="knowledge5[]" value="D" /> 
<input type="image" id="submit" alt="Submit" src="../img/btn_submit.gif" style="border:0px;" />
</form>

I need to make sure the user checks atleast 1 checkbox in each set.

im geusing it will be something like this
Code:
function check(theForm)
{

  if (theForm.knowledge1[].value == "")
  {
    alert("Please check a box");
    theForm.knowledge1[].focus();
    return (false);
  }
else

return true;
}

Thats not working. Do i need to assign an id to each set of check boxes??

Im confused??? again...

Soph
 
The first problem is that you cannot name your checkboxes with the brackets. You will have problems in your javascript with the names as they are. I know this is an issue between PHP and Javascript as PHP uses the [] in some field names.

If you add an ID to each checkbox it makes them easier to check using DOM methods.
I have not tested this but I think it should work.
I start out assuming you will have 5 sets of checkboxes to test so the outer loop will run 5 times checking groups from knowledge1 to knowledge5.
The inside loop starts out assuming no boxes are checked setting passed = false. It then loops through each field and if it finds one checked it sets passed = true. At the end of the loop it evaluates passed and if the group had no checked boxes it exits the function returning false since there is no need to continue checking other groups if every group must have at least one check.
If all groups are tested without a group failing then it returns true.

Code:
function chkboxes() {
  for (var x=1; x=5; x++) {
    var chkbox = document.getElementById('knowledge'+x);
    var passed = false; //Defaults to false
    for (var y=0; x<chkbox.length; x++) {
      if (chkbox[y].checked) {
        passed = true;
      }
    }
    if (!passed)
      return false;
  }
  return true;
}



It's hard to think outside the box when I'm trapped in a cubicle.
 
hi niteowl,

thanks for the post. i need to keep the name="knowledge1[] as im storing them as arrays.

I cant get your code to work at the mo.
This is waht i have

Code:
	<script type="text/javascript">
	function chkboxes() {
  for (var x=1; x=2; x++) {
    var chkbox = document.getElementById('knowledge'+x);
    var passed = false; //Defaults to false
    for (var y=0; x<chkbox.length; x++) {
      if (chkbox[y].checked) {
        passed = true;
      }
    }
    if (!passed)
      return false;
  }
  return true;
}
</script>
Code:
<form action="submit.php" method="post" onsubmit="return check(this)">

<input type="checkbox" id="knowledge1" name="knowledge1[]" value="A" />
<input type="checkbox" id="knowledge1" name="knowledge1[]" value="B" />
<input type="checkbox" id="knowledge1" name="knowledge1[]" value="C" />
<input type="checkbox" id="knowledge1" name="knowledge1[]" value="D" />
<br>
<input type="checkbox" id="knowledge2" name="knowledge2[]" value="A" />
<input type="checkbox" id="knowledge2" name="knowledge2[]" value="B" />
<input type="checkbox" id="knowledge2" name="knowledge2[]" value="C" />
<input type="checkbox" id="knowledge2" name="knowledge2[]" value="D" />
<br>

<input type="image" id="submit" alt="Submit" src="../img/btn_submit.gif" style="border:0px;" />
</form>

any ideas, as i have small amount of knowledge when it comes to javascript.

Thanks again

Soph
 
As I said, the [] are illegal in Javascript as part of a file name so the code is not going to work. There is a workaround for this problem on the PHP side but I am not familiar with it.



It's hard to think outside the box when I'm trapped in a cubicle.
 
I did some searching and found another recent thread talking about the same problem here: thread216-1194241

Unfortunately it does not answer the problem. You will have to get someone in the PHP forum to show you how you can access those field names that way you need to in PHP without using the brackets or the Javascript is going to give you headaches.



It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top