SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple checkboxes with array problem

    Hi! I have an array of checkboxes inside a PHP for loop:

    PHP Code:
    <input type="checkbox" name="deleteChkBox[]" id="" value="' . $_SESSION['Data'][$i]['Id'] . '"/> 
    I need the deleteChkBox with the brackets because I want to submit it as an array to PHP in another page. I also have another checkbox to highlight all the checkboxes mentioned above.

    Code HTML4Strict:
    <input type="checkbox" name="deleteAll" onclick="checkAll(document.myform.deleteChkBox[])">

    Problem is Javascript gives me a error. I think it cannot have the brackets in the onclick function. My checkAll javascript function is:

    Code JavaScript:
    function checkAll(checkBox){
    	if (document.myform.deleteAll.checked == true) {
    		for (var i=0; i < checkBox.length; i++) {
    			checkBox[i].checked = true;
    		}
    	} else {
    		for (var i=0; i < checkBox.length; i++) {
    			checkBox[i].checked = false;
    		}
    	}
    }

    How do I have the array in checkboxes and at the same time pass the name to the javascript function? Please help. Thanks.

  2. #2
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    Try this:

    PHP Code:
    <html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <
    script type="text/JavaScript">
    function 
    checkAll(){
        var 
    targetdocument.getElementById('myform');
        var 
    chkBoxListtarget.elements['deleteChkBox[]'];
        var 
    chkBoxLengthchkBoxList.length;
        if (
    target.elements['deleteAll'].checked == true) {
            for (var 
    i=0chkBoxLengthi++) {
                
    chkBoxList[i].checked true;
            }
        } else {
            for (var 
    i=0chkBoxLengthi++) {
                
    chkBoxList[i].checked false;
            }
        }
    }
    </script>
    </head>

    <body>
    <form action="" method="post" id="myform">
    <input name="deleteChkBox[]" type="checkbox" value="1" />
    <input name="deleteChkBox[]" type="checkbox" value="2" />
    <br />
    <input type="checkbox" name="deleteAll" onclick="checkAll()">
    </form>
    </body>
    </html> 
    Bye.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But it doesn't work in Firefox. It says:

    Error: document.myform.elements is not a function

    OKay in IE. Anyway to get around it?

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's okay now in Firefox. I did not separate the getElementById and elements.

    Thanks.


Bookmarks

Posting Permissions

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