Forums

Set Checkbox Checked/Unchecked based on database field

gotpowr 31 Jul, 2024

Chronoforms7 -I have a registration form with a few slider checkboxes.  Value set to 1, ghost value set to 0.

Data saves correctly (1/0), but I cannot get the checkboxes to load properly.

If I put the code in the "checked column", nothing happens {var:model1.checkboxfield}

I looked all over and am having trouble seeing anywhere to do a "set checkboxfield1 as checked"

I've googled, searched the forums, read the faqs and can't find anything with Chronoforms7.

Eddi Bournell 06 Aug, 2024

I make it like this: 

into the Checkbox`  Field Name    I put    Model.checkboxfieldname

where checkboxfieldname is the name of the DB-tablefield type of varchar(1) .

If the DB-Field is  type of  JSON

I put this into the Checked-Option:

{data:set:checkboxname$(var:read_data_action.Model.fieldnameOfTheBox)}

In the read_data_action add Special Field and set Processing Function to  Decode JSON

(Thanks to Dieter Woellner)

But this works when  DB-Field is  type of  tinyInt(1) and no Special Field as well.

gotpowr 07 Aug, 2024

Thanks for the response!

I might be doing something wrong because it's not working.

I have a read action labeled "Read_Saved_Vehicles"

the model is "savedvehicles"

the db field of "smallint" (1/0) is named "vehicle1_24ghz"

I named the checkbox field name: "vehicle1_24ghz"

I put the following in checked:

{data.set:vehicle1_24ghz$(var:Read_Saved_Vehicles.savedvehicles.vehicle1_24ghz)}

And nothing.  I verified the variable shows properly in the debug and another text field (shows "1")

I've tried changing the name of the checkbox in the box and formula, tried removing the read name, etc. but nothing. 

Eddi Bournell 08 Aug, 2024

There should be be a colon :     after {data:.......

gotpowr 12 Aug, 2024

Sorry I should have specified - I tried it both ways (data: and data.).  When it didn't work with data:, I looked at the short codes cheat sheet and it had data.set mentioned in there.

So I did:

{data.set:vehicle1_24ghz$(var:Read_Saved_Vehicles.savedvehicles.vehicle1_24ghz)}

and

{data:set:vehicle1_24ghz$(var:Read_Saved_Vehicles.savedvehicles.vehicle1_24ghz)}

I also tried:

{data.set:vehicle1_24ghz$(var:read_data_39.savedvehicles.vehicle1_24ghz)}

and

{data:set:vehicle1_24ghz$(var:read_data_39.savedvehicles.vehicle1_24ghz)}

and removing the read name/id and just using the Model.  None seem to be setting the checkbox value to checked.

Eddi Bournell 12 Aug, 2024

This is weird. I guess , you have checked by PhpMyAdmin the status of 'vehicle1_24ghz',  Everything seems to be right. When I had a similar Problem, I deleted the particular action, cleared the cache and started with a new action and same parameters.

Eddi

gotpowr 12 Aug, 2024

Yup, I checked the status.  It's tinyint(1) set to "1" as a value.  If I map it to a textbox it shows 1 properly on the front-end.

I just tried redoing it with a brand new created checkbox and same thing - it's not showing checked even though the value is 1.  I cleared the cache multiple times to hope that it would work.

Eddi Bournell 12 Aug, 2024

Sorry, I don't have a clue, what is wrong with your checkbox / your form. 

Look here: https://www.chronoengine.com/forums/topics/view/108999/set-radio-or-check-box-based-on-value-from-db

and  https://www.chronoengine.com/forums/topics/view/109015/how-to-read-db-array-in-cf7-formfield

gotpowr 13 Aug, 2024

Thanks for the links!  I had searched through Google and could not find those!  

I got it to work.  I used the populate fields and named my checkbox "savedvehicles.vehicle1_24ghz".  I left the "Checked" field set to default (Not checked).

Thanks for your help!

Eddi Bournell 13 Aug, 2024

Congratulation! Now you are an expert. You should write "The Chronoforms Cook Book" for all those thousands of CF7-users who are poking around in the dark. Might be a bestseller ;-) 

You need to login to be able to post a reply.