Adding entries to second form

By: Bob Webb | Asked: 07/17/2023
ForumsCategory: How-toAdding entries to second form
Bob Webb asked 1 year ago
I have a form, A, that has a drop down within a repeatable section populated from another form, B. There's an option allow users to add a new item in the drop down but I didn't want to use the add option so I've added an Add New to the drop down and this enables a new field where the user enters the new value. In addition there's a price related to this item which the user also enters. What I want to do is add the new option and price in form B. I go as far as this add_filter('frm_validate_field_entry', 'copy_my_field', 10, 3);
function copy_my_field($errors, $posted_field, $posted_value){
  if ( $posted_field->id == 50 ) {
              $new_designation = $posted_value;
              $new_base = $_POST['item_meta'][53];
             FrmEntry::create(array(
            'form_id' => 8,
            'item_key' => 'entry', //change entry to a dynamic value if you would like
            'frm_user_id' => $user,
            'item_meta' => array(
                   6621 => $user,
                  30 => $new_designation,
                  57 => $new_base,
                  ),
             ));
 }
return $errors;
}     then realised there was no access to the price field since this hook is only looking at the single field.

Any and all, polite, suggestions appreciated.
2 Answers
Rob LeVineRob LeVine answered 1 year ago
If I understand it correctly, what you want is either/both frm_after_create_entry or frm_after_update_entry, so that you're writing the new information when you're creating or updating that entry. That way you have all the information you need. If you're trying to do it in real time, you'll need to use Ajax to call a PHP function.
Bob Webb replied 1 year ago

Thanks that makes more sense although I'll need to loop through the repeatable sections which I haven't done before. I'll look into that presumably a foreach?

Rob LeVineRob LeVine replied 1 year ago

It's not the most complicated thing ever, however, it's not as easy as you'd hope. I'm on the verge of writing a tutorial on how to extract repeater information. I hope to have it done this week.

Bob Webb replied 1 year ago

I think it can wait and I'll be your guinea pig if you want

Victor Font answered 1 year ago
Actually, you can still use the same filter because the entire PHP $_POST associative array is available to you. This means you can access the price field with $_POST['item_meta'][xx] where xx = price field id.
Bob Webb replied 1 year ago

Thanks again, I shall try that and let you know.

Bob Webb replied 1 year ago

Thanks Victor, still not working for me though
add_filter('frm_validate_field_entry', 'copy_my_field', 10, 3);
function copy_my_field($errors, $posted_field, $posted_value){
if ( $posted_field->id == 50 ) {
$new_designation = $posted_value;
$new_base = $_POST['item_meta'][53];

FrmEntry::create(array(
'form_id' => 8,
'item_key' => 'entry',
'frm_user_id' => $user,
'item_meta' => array(
164 => $user,
30 => $new_designation,
57 => $new_base
),
));
}
return $errors;
}

The new_designation is being created but the base cost is not and neither is the user ID. You're 100% right though I've printed the array and can see everything I need, array within an array for each of the repeatables but I'm getting closer/
I need to work on only running it when a new designation is added so it runs even when the field is blank.

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