Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    testing checkboxes with variable names

    I have a form with several types of checkboxes, and I'm trying to check all but one of them to determine if any of those have been checked. I do not want to check 'all', but I want to see if any of the 'check_' or 'check2_' checkboxes have been checked. There are a variable number of checkboxes, so this is just an example with two of each. Set 1 and Set 2 will always have the same number of checkboxes as each other, though.

    I'm having trouble getting the function to do this - so any guidance would be appreciated.

    Code:
    <script language="JavaScript">
    	function checkForNone() {
    		checked=false;
    		for (i=0,n=selectForm2.elements.length;i<n;i++) {
    		  if (document.selectForm2.getElementById("check_"+String(i)).checked || document.selectForm2.getElementById("check2_"+String(i)).checked ) {
    		    checked=true;
    		  }
    		}  
    		if (checked==true) {
    		  alert("OK!");
    		  return true;
    		}
    		else {
    		  alert("Please select at least one checkbox.");
    		  return false;
    		}
    	}
    	</script>
    PHP Code:
    echo "
    <form name='selectForm2' id='selectForm2'>

    Overall:
    <br>
    <input type='checkbox' name='all' value='' class='resultsAll' id='all'>

    Set 1:
    <br>
    <input type='checkbox' name='check_1' value='01' class='results1' id='check_1'><br>
    <input type='checkbox' name='check_2' value='02' class='results1' id='check_2'><br>
    <br>

    Set 2:
    <br>
    <input type='checkbox' name='check2_1' value='01' class='results2' id='check2_1' disabled='true'><br>
    <input type='checkbox' name='check2_2' value='02' class='results2' id='check2_2'>
    <br><br>
    Check for none selected <input type='button' onclick=\"checkForNone();\" />

    </form>
    "


  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    This should point you the right way:-

    Code:
    <script type = "text/javascript">
    function Check(chk) {
    var totChecked = 0;
    for (i = 0; i < chk.length; i++) {
    if (chk[i].checked == true) {
    totChecked ++
    }
    }
    alert ("Total checked in this set = " + totChecked)
    }
    </script>
    
    
    <form name="myform" >
    
    <input type="checkbox" name="check_list1" value="1">RED<br>
    <input type="checkbox" name="check_list1" value="2">BLUE<br>
    <input type="checkbox" name="check_list1" value="3">GREEN<br>
    <input type="checkbox" name="check_list1" value="4">YELLOW<br>
    
    <input type="button"  value="Count" onClick="Check(document.myform.elements['check_list1'])">
    <br>
    <br>
    <input type="checkbox" name="check_list2" value="1">MEAT<br>
    <input type="checkbox" name="check_list2" value="2">FISH<br>
    <input type="checkbox" name="check_list2" value="3">VEGETABLES<br>
    <input type="checkbox" name="check_list2" value="4">FRUIT<br>
    
    <input type="button"  value="Count" onClick="Check(document.myform.elements['check_list2'])">
    </form>

    Lottery: A tax on people who are bad at math.

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks. I can see how your logic works if all the checkboxes in a set have the same name (like name="check_list1"), but I think part of my problem has to do with the fact that the name and ID on each row are different from the next row in the set (check_1, check_2, check_3), and I don't have the construct right for selecting these elements in order to evaluate each one as it goes through the loop a variable number of times.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    There is nothing to stop you using a different id for each checkbox (and indeed each id must be unique). But all the checkboxes in a group or set should have the same name. That is required because you want to loop through an array a variable number of times.

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Resolved

    Thanks for your help, Philip.

  • #6
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Not resolved

    I spoke too soon.

    The function does work inJavaScript, but when I went back and changed the names of checkboxes to one name in my PHP app, all sorts of things blew up, mainly because I depend on the POSTed names of these variables in PHP.

    I can't go back and rework the PHP just to make this JavaScript work, so I'll need to find a way in JavaScript to work off of the checkboxes with incremental variables names (check_1, check_2, etc.). I'm pretty sure there are ways to do this. I've just been having trouble making it work.

    Thanks to anyone who may know how to do this...

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    For PHP to handle groups of same-named fields, you just have to append [] to the name of the fields.

    So
    Code:
    <input type="checkbox" name="check_list1[]" value="1">RED<br>
    <input type="checkbox" name="check_list1[]" value="2">BLUE<br>
    <input type="checkbox" name="check_list1[]" value="3">GREEN<br>
    <input type="checkbox" name="check_list1[]" value="4">YELLOW<br>
    ...
    <input type="button"  value="Count" onClick="Check(this.form.elements['check_list1[]'])">
    Then you can easily alter the PHP code to use
    Code:
        $_FORM["check_list"][0]
    in place of 
        $_FORM["check_list0"]
    If that's really too difficult, then we *can* hack a fix in JS:
    Code:
    <input type="checkbox" name="check_list1_1" value="1">RED<br>
    <input type="checkbox" name="check_list1_2" value="2">BLUE<br>
    <input type="checkbox" name="check_list1_3" value="3">GREEN<br>
    <input type="checkbox" name="check_list1_4" value="4">YELLOW<br>
    
    <input type="button"  value="Count" onClick="Check(this.form,'check_list1');">
    
    ...
    function Check( form, basename )
    {
        var count = 0;
        for ( var i = 1; i < 9999; ++i )
        {
            var fld = form.elements[basename + "_" + i];
            if ( fld == null ) break; // quit when no more of that basename
            if ( fld.checked ) ++count; 
        }
        alert( basename + " has " + count + " checked";
        return count; // if you want to use it elsewhere
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    alert( basename + " has " + count + " checked" );

  • #9
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Resolved

    This works. Thanks to both Old Pedant and Philip M for your help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •