Page 1 of 1

checkbox issue

Posted: Sat Jan 12, 2008 11:34 am
by asterp
I want to do something in Php and I am kind of stuck so I would greatly appreciate your help. Basically consider that you have an music album of songs. The album name and the song name have one checkbox per entry (i.e for every album name there is a checkbox and for every song name within the album name there is a checkbox) . Now what have done is pulled out entries from database and created php code for displaying all the entries in album. What I want to do is when a user checks the album name box all the individual checkboxes of song name should be disabled and when the user unchecks the album name box all entries should be enabled.

Do you have a way of dealing with this ?

Re: checkbox issue

Posted: Sat Jan 12, 2008 12:31 pm
by VladSun
Yes - use javascript.
You need onclick event and disabled property.

Re: checkbox issue

Posted: Sat Jan 12, 2008 2:09 pm
by RobertGonzalez
Moved to Client Side.

Re: checkbox issue

Posted: Sun Jan 13, 2008 2:15 pm
by Jonah Bron

Code: Select all

 
<input type="checkbox" onchange="if (this.checked)document.getElementById('othercheckbox').disabled=true; else document.getElementById('othercheckbox').disabled=false;" />
 

Re: checkbox issue

Posted: Mon Jan 14, 2008 11:25 am
by pickle
~PHPyoungster's code will work if you only have one checkbox.

Save yourself a TON of grief and use the jQuery library to do this. Set up your code like this:

Code: Select all

<!-- Checkbox for album 123 --><input type = "checkbox" name = "album123" id = "album123" class = "album_checkbox" />......<!-- Checkboxes for songs in album 123 --><input type = "checkbox" name = "album123_song1" class = "album123_song"/><input type = "checkbox" name = "album123_song2" class = "album123_song"/><input type = "checkbox" name = "album123_song3" class = "album123_song"/><input type = "checkbox" name = "album123_song4"  class = "album123_song"/>
The important attributes here are the classes, as that's what you can use in jQuery to reference the different checkboxes.

Code: Select all

 /* Define the "anonymous" function to run when anything with the class "album_checkbox" is clicked */$(".album_checkbox").click(function(){     /* Extract the ID number from the clicked checkbox.  $(this) refers to the checkbox that was clicked.  */    var id = $(this).attr('id').substring(5);     /* If the clicked checkbox is checked, remove the "disabled" attribute" from the song checkboxes in this album */    if($(this).attr('checked'))        $(".album"+id+"_song").removeAttr("disabled");     /* If the checkbox is not checked, disable the song checkboxes in this album */    else        $(".album"+id+"_song").attr("disabled","disabled");});