Ask about CHECKBOX

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Portnumbay
Forum Newbie
Posts: 14
Joined: Mon Jul 05, 2010 1:22 am

Ask about CHECKBOX

Post 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...
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Ask about CHECKBOX

Post 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.
Portnumbay
Forum Newbie
Posts: 14
Joined: Mon Jul 05, 2010 1:22 am

Re: Ask about CHECKBOX

Post 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
Portnumbay
Forum Newbie
Posts: 14
Joined: Mon Jul 05, 2010 1:22 am

Re: Ask about CHECKBOX

Post 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 ?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Ask about CHECKBOX

Post 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'.
Post Reply