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 4 hours 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 3 hours 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 hour 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) . ',';
}

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