Insert form data into second database table with repeater

By: Marek Kiecana | Asked: 01/05/2023
ForumsCategory: How-toInsert form data into second database table with repeater
Marek Kiecana asked 2 years ago

Hi, how do I insert data from a form into another table in the database if there is a repeater field in that form?  

The effect I want to get: 

Form:

field1

repeater field2 field3  

table entry:  

field1 | field2 | field3

_________________

val1   | val2   | val3  

 

My snippet, but he create more than one row in table:  

 

add_action('frm_after_create_entry', 'copy_pz', 20, 2);
function copy_pz($entry_id, $form_id){
  if($form_id == 98){
    global $wpdb;
 foreach( $_POST['item_meta'][910] as $k => $r ) {
if ( is_array( $r ) ) {
foreach ( $r as $i => $v ) {

    $values = array('faktura' => $_POST['item_meta'][909], 'towar' => $v);
        $wpdb->insert('mag_stan', $values);

}
}
}
}
}
      My form:   image.png

Attachments
2 Answers
Best Answer
Marek Kiecana answered 2 years ago

I already managed:  add_action('frm_after_create_entry', 'copy_pz', 20, 2);
function copy_pz($entry_id, $form_id)
{   if($form_id == 98){      global $wpdb;   foreach( $_POST['item_meta'][910] as $k => $r ) { if ( is_array( $r ) ) { foreach ( $r as $i => $v ) { if($i==912){ $towar=$v; }if($i==913){ $ilosc=$v; }if($i==914){ $cena=$v; }if($i==915){ $wartosc=$v; }if($i==915){     $values = array('faktura' => $_POST['item_meta'][909], 'towar' => $towar, 'ilosc' => $ilosc, 'cena' => $cena, 'wartosc' => $wartosc);         $wpdb->insert('mag_stan', $values);} } } } } }

Victor Font replied 2 years ago

Does this mean you have it working? If so, please mark this thread as resolved.

Kratos Bragaa replied 5 months ago

where to add this code.. and which line code id need to be replace?

Victor Font answered 2 years ago
Repeaters are in reality embedded forms into and of themselves. The repeater form is linked to the parent form. Repeater values have 3 dimensions to their names "item_meta[87][0][89]". The first dimension is the repeater field id on the parent form. The second dimension is the row ID. Row ids may or may not be consecutive numbers depending on whether repeater rows have been deleted or not. The third dimension is the field id in the repeater row. The best way to view your data is to view the $_POST associative array so you can see the data you are trying to pursue. For this, I use the Kint debugger. Using Kint, you can set a breakpoint to examine data points while developing your code. It's very useful.

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