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:
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);} } } } } }
Does this mean you have it working? If so, please mark this thread as resolved.
where to add this code.. and which line code id need to be replace?
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.
Please login or Register to submit your answer