Tell us what you are trying to do?
Since switching to PHP 8.1, my code no longer works.
I have several checkbox categories. These checkboxes should be counted within the categories and then added together. The code worked great the whole time. I have now switched to PHP 8.1 and now I get the following error message when submitting the form.
The error appears as soon as no checkboxes are checked in one checkbox category (e.g. wpcf-checkboxes-bauen-renovieren) and it should output the value "null".
---------------------
Warning: Undefined array key "wpcf-checkboxes-bauen-renovieren" in /homepages/15/d845764028/htdocs/clickandbuilds/FirmeninfoRegional/wp-content/toolset-customizations/preis-logoduo.php on line 18
=> $field2 = sizeof($_POST['wpcf-checkboxes-bauen-renovieren']);
Fatal error: Uncaught TypeError: sizeof(): Argument #1 ($value) must be of type Countable|array, null given in /homepages/15/d845764028/htdocs/clickandbuilds/FirmeninfoRegional/wp-content/toolset-customizations/preis-logoduo.php:18
---------------------
Here is the code that I'm using:
add_action('cred_save_data','func_connect_child_posts',15,2);
function func_connect_child_posts($post_id,$form_data) {
if (($form_data['id']==352) or ($form_data['id']==687)) {
if(!empty($_POST['wpcf-checkboxes-auto-verkehr'])) {
update_post_meta($post_id, 'wpcf-count-checkboxes-auto-verkehr', sizeof($_POST['wpcf-checkboxes-auto-verkehr']));
}
}
if (($form_data['id']==352) or ($form_data['id']==687)) {
if(!empty($_POST['wpcf-checkboxes-bauen-renovieren'])) {
update_post_meta($post_id, 'wpcf-count-checkboxes-bauen-renovieren', sizeof($_POST['wpcf-checkboxes-bauen-renovieren']));
}
}
if (($form_data['id']==352) or ($form_data['id']==687)) {
$field1 = sizeof($_POST['wpcf-checkboxes-auto-verkehr']);
$field2 = sizeof($_POST['wpcf-checkboxes-bauen-renovieren']);
$sum = $field1 + $field2;
update_post_meta($post_id, 'wpcf-count-checkboxes-gesamtzahl', $sum);
}
}
How do I have to rewrite the code so that the form works again in PHP 8. Hope you can help me.
Is there any documentation that you are following?
https://toolset.com/forums/topic/how-to-count-number-of-selected-chechboxes/
Is there a similar example that we can see?
No
What is the link to your site?
hidden link