Checked checkboxes from array

181 views
Skip to first unread message

KV_Trevor

unread,
Mar 21, 2013, 12:57:13 PM3/21/13
to php-form-bu...@googlegroups.com
I have an array that I am calling from a database and trying to pass the values back into the checkbox to be marked as checked. This is the array

$goalsArray = Array ( [0] => "regular exercise" [1] => "reduce medications" [2] => "sport specific" [3] => "improve eating habits" [4] => "increase energy" [5] => "rehabilitation" [6] => "reduce stress levels" [7] => "increase strength" [8] => "look better" [9] => "shape/tone" [10] => "increase muscle" [11] => "feel better" [12] => "improve posture" [13] => "flexibility" [14] => "lose weight" [15] => "gain weight" [16] => "burn fat" [17] => "accountability" )

$form->addElement(new Element_Checkbox("Goals:", "goals", $goalsOptions,  array("value" => $goalsArray)));

But nothing gets check, I can think of with no results. Please help

aziz.nev

unread,
Mar 21, 2013, 1:28:53 PM3/21/13
to php-form-bu...@googlegroups.com
Hello,

Try to put $goals instead of "value" in the last parameter of your checkbox element.
Please tell me if it works.

Kind regards,

Aziz

2013/3/21 KV_Trevor <tre...@kleurvision.com>


--
You received this message because you are subscribed to the Google Groups "php-form-builder-class" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-form-builder-...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

aziz.nev

unread,
Mar 21, 2013, 1:31:18 PM3/21/13
to php-form-bu...@googlegroups.com
Question :

Which value of goalsArray must be checked by default ? I would rather have something like this :
$form->addElement(new Element_Checkbox("Goals:", "goals", $goalsOptions,  array("value" => $goalsArray[i]));
where "i" is the element order.

2013/3/21 aziz.nev <aziz...@gmail.com>

KV_Trevor

unread,
Mar 21, 2013, 2:26:55 PM3/21/13
to php-form-bu...@googlegroups.com
By default, nothing is checked. The "checked" values of the array are coming from the database. The values are saved in the database as CSV, but even if I try to pass them in like this:

$goals = "regular exercise", "reduce medications", "sport specific", "improve eating habits", "increase energy", "rehabilitation", "reduce stress levels", "increase strength", "look better", "shape/tone", "increase muscle", "feel better", "improve posture", "flexibility", "lose weight", "gain weight", "burn fat", "accountability"$form->addElement(new Element_Checkbox("Goals:", "goals", $goalsOptions,  array("value" => array($goals)));

Nothing is returned checked.

I even tried what you had about, but this also didn't work.

Thanks for the quick response.
2013/3/21 aziz.nev <aziz...@gmail.com>
Aziz


2013/3/21 KV_Trevor <tre...@kleurvision.com>
To unsubscribe from this group and stop receiving emails from it, send an email to php-form-builder-class+unsub...@googlegroups.com.

Aziz

unread,
Mar 22, 2013, 7:00:47 AM3/22/13
to php-form-bu...@googlegroups.com
When you use a parameter array("value"-> "whatever"), That means that you want to specify a default value. 
If I do not misunderstand , there should be only 3 attributes to construct your checkbox, Goals, goals and the last one is a modified version of your goalsOption array where you put out array keys and you keep only array values. Php fonction for this , I think, array_values, Check on the php help.



Envoyé de mon iPhone
To unsubscribe from this group and stop receiving emails from it, send an email to php-form-builder-...@googlegroups.com.

KV_Trevor

unread,
Mar 22, 2013, 11:15:59 AM3/22/13
to php-form-bu...@googlegroups.com
So it turns out the problem was on my end, for some reason I was storing the CSV with quotes around each section so when I exploded the CSV it was putting all the values in the fist key of the array.

For anyone looking for the same solution this is the answer:

$optionsArray = array("1", "2", "3", "4", "5", "6", "7");
$csv = "1, 2, 3, 4";
$selectedArray = explode(", ",$csv);            
$form->addElement(new Element_Checkbox("Field Label:", "field", $optionsArray, array("value" => $selectedArray)));

Thanks for your help and replies Aziz.

Roberc

unread,
Mar 22, 2013, 11:21:23 AM3/22/13
to php-form-bu...@googlegroups.com
Maybe that's because intval("1") != "1" ? ;-)

Пользователь KV_Trevor <tre...@kleurvision.com> писал:
Reply all
Reply to author
Forward
0 new messages