Page 1 of 1

Ask about CHECKBOX

Posted: Mon Jul 05, 2010 1:51 am
by Portnumbay
Hi Masters.. I am newbie here..
I wanna ask a question about checkbox in php..
I have table for example category in mySQL database. In my php script, i have 4 checkboxes (for example : Music, Game, Book, Handphone). I have a field in the category table for parameter true or false (1 or 0) so when the category that have value "1", the checkbox for the category is checked when the php script loaded..
Its just like a sorting for data.. when user check Game and Music, the page only load data for game and music... but when he unchecked it, the page will reload and only show the category checked...

Re: Ask about CHECKBOX

Posted: Mon Jul 05, 2010 3:29 am
by requinix
So what's the question?

Blanket answer: you can only know which checkboxes were checked. If you want to know which ones were left unchecked the easiest method is to compare the list of everything and subtract from it the list of checked ones; array_diff might help.

Re: Ask about CHECKBOX

Posted: Mon Jul 05, 2010 4:13 am
by Portnumbay
My question is.. whats the script to make the checkbox checked if the value from the database is 1

like in VB.Net there is thing like Checkbox.checked

Re: Ask about CHECKBOX

Posted: Mon Jul 05, 2010 8:18 am
by Portnumbay
Example :
Image

All data will be load at the first time user click the page.. then user can sort it by uncheck and check category they want the data displayed... Can somebody gimme simple example ?

Re: Ask about CHECKBOX

Posted: Mon Jul 05, 2010 6:53 pm
by Jonah Bron
Use a SQL statement to check if it should be checked. Store that in a boolean variable (ex. $should_be_checked = true;). Then, where the checkbox is, do something like this:

Code: Select all

<input type="checkbox" name="somename" value="1" <?php echo $should_be_checked ? 'checked=""' : ''; ?>/>
The presence of the 'checked' attribute in the checkbox tells the browser that it's default value is checked. Same goes for radio buttons, but the attribute is 'selected'.