Why are checkboxes so hard??

minimalniemand 11 May, 2012
Sorry, but I have a really hard time implementing the storing of a single checkbox into the database.

I have custom code in my form like this:

<input value="<?PHP print $jobActive; ?>" title="" type="checkbox" name="jobActive" id="jobActive" class="label_left" checked=\"<?PHP print $jobActive; ?>\" />
<label for="jobActive">Published</label><div class="clear"></div><div id="error-message-jobActive"></div></div>


the database field is named jobActive

I had it a tiny int field (storing 0 or 1) in the database, but haven't got it working. I changed it to varchar(10) holding the text "checked" or "unchecked" (thought it would be easier then). Still, I don't get it.

I've been doing desktop application programming for a while, where checkboxes usually have 2 values (sometimes 3, including NULL) and you just set the value true or false. that's it.

Why this "checked" "unchecked" thing in html/PHP? It seems so complicated!

Sorry for the rant, but I feel really stupid at the moment for not getting a simple checkbox to work 😈
Max_admin 11 May, 2012
Hello,

I'm not sure what you are doing with the checkbox but your code below may not be correct, try to have a fixed value for the checkbox, e.g: 1, and your checkbox will add 1 to the table if its checked, but it will not even be in the submit array if its not checked.

Chronoforms adds a ghost input over checkboxes to make sure that they submit some value even if none is checked, you may test this in a wizard created form.

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
minimalniemand 11 May, 2012

I'm not sure what you are doing with the checkbox



I just want a 1 stored when the user checks the checkbox (checked as default) and a 0 when the user unchecks the checkbox in form A (create record form)

In form B I want to load the checkbox (checked if database field = 1, and unchecked if 0) and then update the checkbox value in the DB - really nothing exotic

[...] try to have a fixed value for the checkbox, e.g: 1, and your checkbox will add 1 to the table if its checked, but it will not even be in the submit array if its not checked.



that was the first thing I tried, like I am used to from desktop programming, then I read about the checked-parameter of html-checkboxes ...

Chronoforms adds a ghost input over checkboxes to make sure that they submit some value even if none is checked, you may test this in a wizard created form.



Ok, that's a good idea. I'll give this a try.
Thanks.
This topic is locked and no more replies can be posted.