SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check All checkboxes problem

    Hi there,

    I would need to implement one of the many check all scripts out there. My problem is that that my checkbox names are like name[] so they end up in an array. Retrieving the value for the javascript function doesn't work like this <input type=button value="Check All" onClick="this.value=check(this.form.name[])">. Using ..this.form.name.. works for the check all function, but my original function (PHP) doesn't work anymore.

    Can somebody help?

    Thanks and best regards

    ASchweti

    PS: Underneith you'll see the function I tried to implement...

    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    var checkflag = "false";
    function check(field) {
    if (checkflag == "false") {
    for (i = 0; i < field.length; i++) {
    field[i].checked = true;}
    checkflag = "true";
    return "Uncheck All"; }
    else {
    for (i = 0; i < field.length; i++) {
    field[i].checked = false; }
    checkflag = "false";
    return "Check All"; }
    }
    // End -->
    </script>

    <input type=button value="Check All" onClick="this.value=check(this.form.list)">

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe something like this would help:
    <html>
    <head>
    <title>Check All</title>
    <script language="JavaScript">
    function docheck(formid,state)
    {
    frmobj = document.forms[formid];
    len=frmobj.length
    for(i=0;i<len;i++)
    {
    if(frmobj.elements[i].type="checkbox")
    {
    frmobj.elements[i].checked=state
    }
    }
    }
    </script>
    </head>
    <body>
    <center>
    <form name="myform">
    <input type="checkbox" name="c0"> Checkbox One
    <br>
    <input type="checkbox" name="c2"> Checkbox Two
    <br>
    <input type="checkbox" name="c3"> Checkbox Three
    <br>
    <a href="javascript:docheck('myform',true)">Check All</a>
    <br>
    <a href="javascript:docheck('myform',false)">Un Check All</a>
    </form>
    </center>
    </body>
    </html>

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi requestcode,

    problem is that all boxes have to be named delete[]. The code must be implemented into a shopping cart. PHP takes it from there, so I doubt that your solution will work in my case?

    What do you think?

    Regards ASchweti

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whenever referencing a form element by name, and that name has non alphanumeric characters, you should use the elements collection for string-based lookups.

    <input type=button value="Check All" onClick="this.value=check(this.form.elements['name[]'])">
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's it!!! Thanks very much solved my problem 100%.

    Have to say this is really a great forum! This was my second problem that I posted here and both were solved within hours ;-)

    Best regards ASchweti

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could I ask one last question? The script uses standard form buttons. Would it be a big deal to use type=image buttons instead (which then should change when clicked)?

    I also realized that the function doesn't check or uncheck, when there is only one item in the cart.

    Well functionality is there, any extra hint would be a bonus :-)

    Thanks again,

    ASchweti

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd do it this way. First, the function
    Code:
    function checkAll( btn, fieldName )
    {
    	var field = btn.form.elements[fieldName];
    	if ( typeof btn.check == 'undefined' ) btn.check = true;
    	if ( typeof field.length == 'undefined' )
    	{
    		field.checked = btn.check;
    	}
    	else
    	{
    		for ( i = 0; i < field.length; i++ )
    		{
    			field[i].checked = btn.check;
    		}
    	}
    	if ( !btn.check )
    	{
    		btn.value   = "Check All";
    		btn.check = true;
    	}
    	else
    	{
    		btn.value   = "Uncheck All";
    		btn.check = false;
    	}
    }
    Then, the HTML
    Code:
    <input type="button" value="Check All" onclick="checkAll( this, 'list[]' )"/>
    <input type="checkbox" name="list[]"/>
    Don't use type=image buttons - they act just like submit buttons (meaning, when you click them, they submit the form)
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    works great. I will use some CSS for the button so.

    I should definitively learn more Javascript.

    I always play around until I bang my head on the wall, and you post a working solution ´within minutes.

    Well, I have my strength in PHP ;-)

    Regards ASchweti

  9. #9
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ASchweti
    I always play around until I bang my head on the wall, and you post a working solution ´within minutes.
    It's just experience, that's all. It will come to you with time.

    Oh, and you're quite welcome
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •