Forums

Populate checkboxes with data from array

michaelFFW 20 Oct, 2019
Hi,
I have a form where it pre-populates the checkboxes from a database table.
I can retrieve the values fine but unable to get the checkboxes checked .

I use the "Checked options values" field but it seems the values each must be on a new line.
The array displays it as A,B,C which does not work.

Any idea how to do this?
Thanks
GreyHead 21 Oct, 2019
Hi Michael,

I think that you can leave the Checked options values box empty - that is for when you want to set some options always pre-selected.

ChronoForms should check the boxes from the read Data - the only problem might be that if this is saved as a string it may need to be re-converted back to an array.

Bob
michaelFFW 21 Oct, 2019
Hi,
I converted the string into an array as you suggested.

read_data1.Data1.ride_vol value in debug is
  [ride_vol] => S,T,A
In php action I converted to array.
$ridevol = $this->get("read_data1.Data1.ride_vol");
$arrayRideVol = explode (",", $ridevol);
$ride_vol = $arrayRideVol;
return $ride_vol;

This shows in debug as
 [ride_vol] => Array
        (
            [returned] => Array
                (
                    [0] => S
                    [1] => T
                    [2] => A
                )

            [var] => Array
                (
                    [0] => S
                    [1] => T
                    [2] => A
                )

        )

In the checkboxes Name and ID field is ride_vol.
I also tried with Name as ride_vol[].

None of the boxes shows checked.

Seems like I am missing something.
Thanks
Michael
GreyHead 21 Oct, 2019
Hi Michael,

I'm never completely sure how this works in CFv6 - it looks as though the data is now a sub-=array of ride_vol. Please try replacing the last two lines of your code with
$this->data('ride_vol') = $arrayRideVol;

Bob
healyhatman 21 Oct, 2019
If you're storing it as a string values should be
{var.split[,]:read_data1.Data1.ride_vol}

Now as to whether or not they're checked, I don't see anything in there to determine one way or the other? Or do you simply want them all to be checked on load all the time?
michaelFFW 21 Oct, 2019
Hi,
This is a form for people to renew membership so I need the checkboxes to be checked based on what they had checked when 1st registering.
michaelFFW 21 Oct, 2019
I put {var.split[,]:read_data1.Data1.ride_vol} in the "Checked options values" field and it now checks the boxes correctly.

I will test some more and let you know how it goes.

Much appreciate the help from you both.

Michael
michaelFFW 21 Oct, 2019
OK, so that didnt work.
Not sure still how to pre-populate boxes with value from table but then still be able to change it in the form.
Tempted to just do all the check boxes in php like I did in CF v4.
GreyHead 21 Oct, 2019
Hi Michael,

What exactly didn't work?

Bob
michaelFFW 21 Oct, 2019
Hi, Sorry for not explaining.
When I put {var.split[,]:read_data1.Data1.ride_vol} in the "Checked options values" field it did check boxes on page load, however, if I then changed a checkbox it didnt update correctly.. Looks like it always checked the initial boxes plus the new ones. If I unchecked an initial box it ignored it.

I was not sure what you meant when you said replace last 2 lines. I did that but got a php error.

I ended up just not populating the checkboxes with past values.

Would be better if I could but if not I'm ok with it.

Michael
healyhatman 22 Oct, 2019
If you change a checkbox, are you remembering to actually SAVE the changes? What's your checkboxes field called? What's the debug say?
michaelFFW 22 Oct, 2019
Hi,
With {var.split[,]:read_data1.Data1.ride_vol} in Checked Options Values box.

Load the form and debug shows
  [ride_vol] => S,T,A
The form is pre-populated with S T A checked.
I now check B also. Keeping other 3 checked. Then click Submit which takes me to next page which shows the form template to verify all is filled out correctly and the A B S T now checked.
Debug shows
 [ride_vol] => Array
        (
            [0] => A
            [1] => B
            [2] => S
            [3] => T
        )
I then decide something is wrong so I click the back button on form which is a history back js.
I then uncheck the A and B and check C. So only C S T is now checked. Click submit to verify page and
Now debug shows
 [ride_vol] => Array
        (
            [0] => C
            [1] => S
            [2] => T
            [3] => T
        )
The verify page shows C S T T.
And the Success page debug shows
[ride_vol] => Array
        (
            [0] => C
            [1] => S
            [2] => T
            [3] => T
        )
All others fields work correctly if I click back button and make changes. The changes go forward correctly. This is text fields and radio buttons and single check box.
Seems only the multi-checkbox has the issue.

Sorry for long winded explanation but hope it helps.
Thanks
Michael
healyhatman 22 Oct, 2019
If you're storing it as a CSV string again make sure you're doing something along the lines of {data.join[,]:ride_vol}
michaelFFW 22 Oct, 2019
Thanks, this helps.
I will be saving as a string again.
Michael
This topic is locked and no more replies can be posted.