Skip Navigation

[Resolved] Getting the label of generic checkboxes in php

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can i get the values of chekcboxes from a CRED form in PHP?

Solution:
It's Custom code, and requires complex understanding of it.
Here is a comprehensive instructions on it:
https://toolset.com/forums/topic/getting-the-label-of-generic-checkboxes-in-php/#post-505850

This support ticket is created 7 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by nabils 7 years, 7 months ago.

Assisted by: Beda.

Author
Posts
#505748

Hi
I am trying to: get the label and value of a generic field of type Checkboxes in php. I am getting the value but how to get the label.
Here my generic field:
[cred_generic_field field='damages' type='checkboxes' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[
{"value":"40","label":"bed"},
{"value":"30","label":"desk"}
]
}
[/cred_generic_field]

Here my php code
$checkboxes = $_POST["damages"];

$ss=0;

foreach ($checkboxes as $checkbox) {
$ss+= $checkbox;
//I need here to get the label as well!!

}
Thank you for helping

#505850

It is not possible to get those Labels in the $_POST data.

Generic Fields are not persisting, they are present only in the Form.

Those fields are stored only in the Post Table in the Database, against the Form itself (which is basically a Post), not against the Post that you edit, or Create with this CRED Form.

This means you will find the IDof your CRED Form in the Post Type "cred-form" in the database, and the Generic Field is in the Post Content column, just as you see it in the CRED Form.

To find the single values, you can var_dump() your $_POST values on a form submit, like this below:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form

        $post_data = $_POST;
        $field = $_POST ["custom_boxes"];


        var_dump($post_data);
	var_dump($field);
die;

}

This above will (on a form with a Generic Checkboxes Field with name "custom_boxes"), submit the form, abort, and print all values of $_POST and it's field "custom_boxes".

In the $_POST you will have this array:

array (size=12)
  'custom_boxes' => 
    array (size=1)
      0 => string 'valueone' (length=8)
  'wpcf-email' => string '' (length=0)
  'new_tax_text_category' => string '' (length=0)
  'new_tax_select_category' => string '-1' (length=2)
  'post_tag' => string '' (length=0)
  'tmp_post_tag' => string '' (length=0)
  'form_submit_1' => string 'Submit' (length=6)
  '_cred_cred_wpnonce_cred_form_4' => string '9d67ef65c5' (length=10)
  '_cred_cred_prefix_post_id' => string '13' (length=2)
  '_cred_cred_prefix_cred_container_id' => string '2' (length=1)
  '_cred_cred_prefix_form_id' => string '4' (length=1)
  '_cred_cred_prefix_form_count' => string '0' (length=1)

As you see the Generic Field is:

 'custom_boxes' => 
    array (size=1)
      0 => string 'valueone' (length=8)

(only the checked field is present in this Data)
There is no label, only the value.

For the Field itself, the dump is as this:

/Applications/MAMP/htdocs/Stable/sub/wp-content/themes/twentyseventeen/functions.php:578:
array (size=1)
  0 => string 'valueone' (length=8)

Again, the same array, and no label.

#505851

Thanks Beda for the explanation, I got it.
So I tried to change my generic field with a custom type field (checkboxes). However, I am not getting the values of selected boxes easily.

$checkboxes = $_POST["wpcf-damages"];
foreach ($checkboxes as $checkbox) {
if ($checkbox == "wpcf-fields-checkboxes-option-95fc0203bf5313b706b099bd9b4d2339-1") {
//here I want to read the value but not possible
}
if ($checkbox == "wpcf-fields-checkboxes-option-ecafabd7520f744c028bb3c010536938-1") {...}

The code works but it is not flexible. If in the future I have to add other choices in the checkboxes, I will have to add another if...then. In addition, if some values of the options change, I will have to change my php code.
I tried all solutions in the forum but nothing works so far

#506101

Checkboxes Fields are complex arrays in this case and the single options are as you see random values.

This is custom code, that I cannot write for you.

But, here is an extensive example with all possible Fields of Types, updated through a CRED Form.
It has inline comments.
Don't let you confuse by the particular name giving of the single fields, this is just because the Code is from something I wrote in past.
Checkboxes are also included:
http://pastebin.com/wz6dQVBE

This will not work with Generic Fields the same way, but you already switched to Types Checkboxes.

#508095

Thanks,
I will try this