$_POST['item_meta'][(field_id)] not pulling in any data

By: Chris Andrews | Asked: 05/14/2022
ForumsCategory: Code Help$_POST['item_meta'][(field_id)] not pulling in any data
Chris Andrews asked 3 years ago
I have a repeating field where users can upload images and then add the alt text to each image in an text field next to the download field. I've used the example here: https://formidableforms.com/knowledgebase/frm_after_create_entry/#kb-add-image-meta and come up with the code below. I took this part: $_POST['item_meta'][(field_id)] from the example, but it's not pulling in any values. Any thoughts on why and what I need to change? Thanks, Chris add_action('frm_after_create_entry', 'add_uploaded_file_alt', 30, 2); add_action('frm_after_update_entry', 'add_uploaded_file_alt', 10, 2); function add_uploaded_file_alt( $entry_id, $form_id ) { if ( $form_id == 3 ) { //(works) $media_ids = array ('64767','64764'); //(works) $alt_text = array ('test 1','test 2'); //however, $media_ids and $alt_text are empty if I //try to get them using ($_POST['item_meta'][(field_id)]) //as per below $media_ids = array ($_POST['item_meta'][88]); $alt_text = array ($_POST['item_meta'][91]); $img_meta = array_combine($media_ids, $alt_text); foreach ($img_meta as $id => $alt) { update_post_meta( $id, '_wp_attachment_image_alt', $alt ); } } }
2 Answers
Victor Font answered 3 years ago

The '(field_id)' is not a value. It's a placeholder that you substitute with the actual field id of your target field. The first character of PHP variables is always a dollar sign ($variable_name).
You can examine the contents of the PHP $_POST associative array using vardump() or the Kint debugger.

Another way to get the actual input name is to examine the page's source code in the browser using its built-in inspection tool.

Look at this example from the Firefox inspection tool:
Imput name shown in Firefox inspection tool.

The circled content is the input name. In this example the input name is item_meta[967]. When captured in the $_POST array, this will be $_POST[item_meta][967]. As a tip, always look for the name to determine what to extract from the $_POST. If this was a checkbox, it would have a third dimension. The input name would be item_meta[967][]. The $_POST variable would then be $_POST[item_meta][967][].

Chris Andrews replied 3 years ago

Ah, thank you Victor - the tip to look at the source code item_meta gave me a hint of what I am dealing with...

I've got a text input in a repeating field, so the item_meta details are like:

item_meta[89][i452][91]

It looks like the 89 is the repeater, 91 is the text field... that i452 IDs the specific field that's been repeated.

If I want to get an array of all of the values added to that text field (91) in the repeated field, (for example, if '1' is entered into the first text field, '2' in the repeated text field, and I want to get an array containing 1,2) how would I go about that since I don't know what the 'i' number will be?

Thanks again!

Chris

Victor Font replied 3 years ago

You have to loop through the $_POST array and add matching elements to a new array. Here's an example: https://stackoverflow.com/questions/21750478/retrieve-post-array-values

Chris Andrews answered 2 years ago
Toyin was kind enough to help me with this and FF is going to use it as an example in their documentation.   One thing leads to another though... I found that while I now have the ability to use the repeater field to upload images and add alt text to the image (an upload field and text field for the alt text in each repeater), the images aren't attached to the wordpress post I'm creating/editing, so my gallery plugin doesn't recognize and display them.    I know just enough php to be dangerous... I'm not even sure if the post id is available in this function, and haven't been able to find any examples of attaching an image to the post in this context that I understand.    Here's what I have so far (with help from Toyin):
 
add_filter('frm_validate_field_entry', 'add_alt_text_form_images', 10, 4);
function add_alt_text_form_images($errors, $posted_field, $posted_value, $args){
if ( $posted_field->id == 14548 ) { // Please replace 14548 with the ID of your repeater field
$image_field_id=14550; // change 5844 to the ID of the file upload field $alt_text_field_id=14551; // change 14551 to the ID of the alt text field $vals= array(); if ( ! isset( $_POST['item_meta'][ $posted_field->id ] ) ) { return $errors; } foreach ( $_POST['item_meta'][ $posted_field->id ] as $row_num => $section_vals ) { if ( $row_num === 'form' ) { continue; } $uploadedimage = $_POST['item_meta'][ $posted_field->id ][$row_num][$image_field_id]; $alttext = $_POST['item_meta'][ $posted_field->id ][$row_num][$alt_text_field_id]; foreach ( (array)$uploadedimage as $id ) { if ( ! $id ) { continue; } update_post_meta( $id, '_wp_attachment_image_alt', $alttext ); } } } return $errors; }
  Any additional help with getting this finished would be appreciated!   

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