Search code examples
phpwordpressgravity-forms-plugingravityforms

GFAPI submit_form() function in loop


I'm struggling with GFAPI's function submit_form() when it's used in loops. For unknown reason it often merges data from other loops into one, the outcome of this situation is that only the very first entry is added in a proper way, the rest seems to be empty.

I can't use other function although I've tried - and it worked (add_entry() for example). I need to use QR generation and attach them to the notification, and these codes are generated when the form is submitted.

CSV file has at least 3 columns: email, full_name and phone_number. Here's my code:

function generateData($filename, $date, $id){
  $rows   = array_map('str_getcsv', file($filename));
  $header = array_shift($rows);
  $csv    = array();

  foreach($rows as $row) {
      $csv[] = array_combine($header, $row);
  }

  $count_array = count($csv);
  for ($i=0; $i < $count_array; $i++) {

    foreach ($csv[$i] as $key => $value) {
      // delete rows that we don't need
      if ($key != 'email' && $key != 'full_name' && $key != 'phone_number') {
        unset($csv[$i][$key]);
      }

    }
  }

  insertGFAPI($csv);
}
function insertGFAPI($entries){
  $count_entries = count($entries);

  for ($i=0; $i < $count_entries; $i++) {

    $data[$i]['input_1'] = $entries[$i]['email'];
    $data[$i]['input_2'] = $entries[$i]['full_name'];
    $data[$i]['input_3'] = $entries[$i]['phone_number'];

    $result = GFAPI::submit_form( get_option('form-id'), $data[$i]);
  }

The outcome that I'd like to get is pretty simple - I want to know why and how is it possible that submit_form() merges data from other loops and how I can prevent it. Do you know what can I do with that?


Solution

  • Solved. It was necessary to empty $_POST array.