Skip Navigation

[Resolved] How to count number of selected chechboxes

This support ticket is created 4 years, 3 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by Jennifer 4 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#1796181

Tell us what you are drying to do

I have a post form with a checkbox field with 20 checkboxes (field-slug: checkboxes-auto-verkehr). I'm trying to count the total number of checked boxes after the form was submitted.

In the second step, I would like to display the number in a new single-line field (field snail: count-auto-traffic) in a second post form.

The Code that I found only counts the number inside a nested repeatable field group.
How can I adapt the code to work for my issue?

Hope you can help me with this.


add_shortcode('checked-count', function () {
 
    global $post;
 
    // slug of nested RFG
    $nested = 'fishspecies';
 
    // slug of checkboxes field
    $field = 'species-availability';
 
    $field = 'wpcf-'.$field;
 
    // Get the nested RFG posts
    $nested_posts = toolset_get_related_posts( $post->ID, $nested, array(
        'query_by_role' => 'parent',
        'role_to_return' => 'child',
        'limit' => 999,
    ) );
 
    $checked = 0;
    foreach ($nested_posts as $key => $nested_post) {
 
        $checkboxes = get_post_meta( $nested_post, $field, true );
        $checkboxes_array = maybe_unserialize( $checkboxes );
 
        $checked += count( $checkboxes_array );
    }
 
    return $checked;
});

Is there any documentation that you are following?
https://toolset.com/forums/topic/how-to-count-number-of-selected-checkboxes-inside-a-nested-repeatable-group/

Is there a similar example that we can see?
No

What is the link to your site?
hidden link

Thanks,
Jenny

#1796461

Hi Jenny,

Thank you for contacting us and I'd be happy to assist.

To better understand the requirement and suggest the best way forward, I'll need to see how this checkboxes field and the two forms are set up.

Can you please share temporary admin login details along with the link to pages where these two forms can be seen?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1796827

Thank you for sharing these details and the admin access.

I'll be performing some tests on my website, using similar fields and the forms, and will share my findings, as soon as this testing completes.

Thank you for your patience.

#1802643

Thank you for waiting.

I apologize for the delay in getting back on this, as we had an unusually busy queue.

During testing on my website, I was able to achieve this using the "cred_save_data" hook:
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data )


add_action('cred_save_data','func_connect_child_posts',15,2);
function func_connect_child_posts($post_id,$form_data) {
	if ($form_data['id']==123) {
		if(!empty($_POST['wpcf-post-checkboxes'])) {
				update_post_meta($post_id, 'wpcf-post-checkboxes-count', sizeof($_POST['wpcf-post-checkboxes']));
		}
	}
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

Note: You'll replace "123" with the actual form ID of the first form that creates the post and "post-checkboxes" and "post-checkboxes-count" with the actual slug of the checkboxes and the checkboxes count fields.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1802987

My issue is resolved now. Thank you!