# Create & Update CSV on form submission \[RESOLVED\]

**URL:** <https://community.gravityforms.com/t/create-update-csv-on-form-submission-resolved/570>\
**Category:** Get Help\
**Tags:** custom, qlik, csv\
**Created:** [December 11, 2018, 6:16pm UTC](https://community.gravityforms.com/t/create-update-csv-on-form-submission-resolved/570 "2018-12-11T18:16:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fldbpr](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/fldbpr/32/242_2.png) [@fldbpr](https://community.gravityforms.com/u/fldbpr)\
**Post date:** [December 11, 2018, 6:16pm UTC](https://community.gravityforms.com/t/create-update-csv-on-form-submission-resolved/570/1 "2018-12-11T18:16:59Z")

</div>

I am attempting to create and update a CSV file to store form submissions. Every time there is a new submission, I would like to append the entries to a new line. In this case, I am only interested in form 11.

I wrote a function(populate\_csv) to open/create a CSV, create the headers, and populate form data from three fields (1,3,4).

My function created the csv and headers, but no data is being populated from the three fields I have specified. I

[Here is a link](https://pastebin.com/NCGHSMpc) to the code I have added to functions.php

Any advice is appreciated.

---

<div class="post-metadata">

**Author:** ![fldbpr](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/fldbpr/32/242_2.png) [@fldbpr](https://community.gravityforms.com/u/fldbpr)\
**Post date:** [December 11, 2018, 9:14pm UTC](https://community.gravityforms.com/t/create-update-csv-on-form-submission-resolved/570/2 "2018-12-11T21:14:46Z")

</div>

I dropped the foreach loop and tried using fputcsv on $data directly. Now, the header and $data print to the csv file, however Field ID#4 is not printing.

Is there an issue with the way I am trying to build an array when one of the objects is a checkbox?

[Here is my updated code](https://pastebin.com/MPN2DRFZ)

Here is a screenshot of my form. Field are 1,4,3 respectively

 ![image](https://us1.discourse-cdn.com/flex020/uploads/gravityforms1/original/2X/e/e53c706f134c3dc51e956298ca402495f8d5f25b.png)

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [December 11, 2018, 10:48pm UTC](https://community.gravityforms.com/t/create-update-csv-on-form-submission-resolved/570/3 "2018-12-11T22:48:16Z")

</div>

What filter are you using? Can you add that to the updated pastebin please?

---

<div class="post-metadata">

**Author:** ![fldbpr](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/fldbpr/32/242_2.png) [@fldbpr](https://community.gravityforms.com/u/fldbpr)\
**Post date:** [December 12, 2018, 2:21am UTC](https://community.gravityforms.com/t/create-update-csv-on-form-submission-resolved/570/4 "2018-12-12T02:21:17Z")

</div>

Hey @chrishajer ,

Sorry I didn’t include that in my second link. I’m using the same one as the first link. [Here is what I currently have](https://pastebin.com/tUmPaH9D).

---

<div class="post-metadata">

**Author:** ![fldbpr](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/fldbpr/32/242_2.png) [@fldbpr](https://community.gravityforms.com/u/fldbpr)\
**Post date:** [December 12, 2018, 3:45pm UTC](https://community.gravityforms.com/t/create-update-csv-on-form-submission-resolved/570/5 "2018-12-12T15:45:25Z")

</div>

I’m getting very close here. I realized I made a mistake in using the wrong mode for ‘fopen’ I was using the ‘w’ parameter instead of ‘a’. Now each new entry is being appended. Unfortunately, the way I wrote this function, it is also appending the headers for each entry. I will work on a solution for this now.

I also figured out why I was not getting data from the checkbox field. I was only referencing the field ID and not the checkbox ID’s. I am going to work on re-writing my code to build ‘Reason’ into an array so that I do not have three columns and headers for one field.

[Here](https://pastebin.com/xDsHi70s) is my latest code if anyone is interested

---

<div class="post-metadata">

**Author:** ![fldbpr](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/fldbpr/32/242_2.png) [@fldbpr](https://community.gravityforms.com/u/fldbpr)\
**Post date:** [December 12, 2018, 8:58pm UTC](https://community.gravityforms.com/t/create-update-csv-on-form-submission-resolved/570/6 "2018-12-12T20:58:57Z")

</div>

For future reference, below is the code I ended up with. It looks to see if the CSV file exists - If not, the headers are created and related data appended. If the file exists, the form data is simply appended to a new row in the CSV file.

The reason behind creating this script is to feed real-time form data to our Qlik server. With this data, we can display a “prettified” version to almost any digital medium.

```
function populate_csv( $entry, $form ) {

//Headers info
$headers = array('Nominee', 'Reason1', 'Reason2', 'Reason3', 'Justification');

//Build form data
$data = array(
    'Nominee' => rgar( $entry, '1' ),
    'Reason1' => rgar( $entry, '4.1' ),
    'Reason2' => rgar( $entry, '4.2' ),
    'Reason3' => rgar( $entry, '4.3' ),
    'Justification' => rgar( $entry, '3' ), 
             );

// check if the file exists or not to determine if headers are needed
$headersNeeded = !file_exists('Nominations.csv');

//Open or Create CSV File
$fh = fopen('Nominations.csv', 'a');

// if headers are needed, add them  
if ($headersNeeded){
    //Create headers
    fputcsv($fh, $headers);
}
//Populate the data 
fputcsv($fh,$data);

//Close the file
fclose($fh);
}
add_action( 'gform_after_submission_11', 'populate_csv', 10, 2 );
```

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [December 12, 2018, 11:47pm UTC](https://community.gravityforms.com/t/create-update-csv-on-form-submission-resolved/570/7 "2018-12-12T23:47:07Z")

</div>

Good deal! Thank you for sharing.

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [December 13, 2018, 2:26pm UTC](https://community.gravityforms.com/t/create-update-csv-on-form-submission-resolved/570/8 "2018-12-13T14:26:54Z")

</div>


