Trying to get value of checkbox with php to send datastring

By: Jen G | Asked: 12/12/2024
ForumsCategory: Code HelpTrying to get value of checkbox with php to send datastring
Jen G asked 1 month ago
Hi, this is probably an easy fix but I've been struggling with this line of php that isn't working for too long.  I need to append the value of all the checked boxes in a multiselect. Below is the code I currently have in place but for some reason this portion always shows up empty in the other CMS I'm posting to. Possibly it needs different formatting but this has been working for all the other fields in the form that I've been grabbing the values for all the fields.
$interest = str_replace(' ', '+', strip_tags($_POST['item_meta'][37]), $i);
Thank you
Attachments
1 Answers
Victor Font answered 1 month ago
Do you know how multiselect checkboxes transmit their data in $_POST? They are sent through as arrays. You have to loop through an array to replace the characters. In your example, your $_POST field should be $_POST['item_meta'][37][]. Depending on the length of the array, your offsets being with 0. If you have 2 boxes checked, the values will be found in $_POST['item_meta'][37][0] and $_POST['item_meta'][37][1]. Hope this helps.
Jen G replied 1 month ago

Oh okay, I didn't even think of it as being treated as an array. Thank you! So then if I want to check for each item selected I could just iterate through the array or would I have to check each key-value for a boolean?

foreach ($interestArray as $value) {
$interest .= strip_tags($value) . ',';
}

Victor Font replied 1 month ago

I suggest you use the Kint debugger. If only a single checkbox is checked, it's sent as a string. https://formidable-masterminds.com/downloads/kint-debugger/

Jen G replied 4 weeks ago

Hi, so that field uses multiple checkboxes. I did try imploding the array instead to make it into a string that can be appended but that hadn't worked either... I did check out the Kint debugger but I don't think I understand enough programming for it to be helpful

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right