# How to attach current form data to populate CSV and add to notifications \[RESOLVED\]

**URL:** <https://community.gravityforms.com/t/how-to-attach-current-form-data-to-populate-csv-and-add-to-notifications-resolved/8315>\
**Category:** Get Help\
**Created:** [March 15, 2021, 12:38am UTC](https://community.gravityforms.com/t/how-to-attach-current-form-data-to-populate-csv-and-add-to-notifications-resolved/8315 "2021-03-15T00:38:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![user603de2cfd8659270](https://avatars.discourse-cdn.com/v4/letter/u/e47c2d/32.png) [@user603de2cfd8659270](https://community.gravityforms.com/u/user603de2cfd8659270)\
**Post date:** [March 15, 2021, 12:38am UTC](https://community.gravityforms.com/t/how-to-attach-current-form-data-to-populate-csv-and-add-to-notifications-resolved/8315/1 "2021-03-15T00:38:57Z")

</div>

Hi all, I’m busy working on populating a CSV file with form entries. I got the code to work and attaches the CSV file to the notification, however, the attachment seems to be from the previous form submission. For example, I submit a form with the name John. The very first test that I received, the CSV contained John. I then send a second test with the name Ben, However, the attached CSV still contains the name, John. The email updates though with Ben. I then run a third test and have the name Micheal. The notification now updates the CSV to contain Ben, not Micheal. Yet the data in the email is Micheal.

How do i get it so the attachment updates on the current form submission?

Here is my code:

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

//Headers info
$headers = array('Name', 'Surname', 'Email', 'Phone', 'Business Name', 'Address (Street Address)', 'Address (Address Line 2)', 'Address (City)', 'Address (State / Province)', 'Address (ZIP/ Postal Code)', 'Address (Cuntry)', 'Donation Amount', 'Donation Type', 'Hear more?', 'How did you hear about us?');

//Build form data
$data = array(
    'Name' => rgar( $entry, '12' ),
    'Surname' => rgar( $entry, '13' ),
    'Email' => rgar( $entry, '5' ),
		'Phone' => rgar( $entry, '8'),
		'Business Name' => rgar( $entry, '6'),
		'Address (Street Address)' => rgar( $entry, '9.1' ),
		'Address (Address Line 2)' => rgar( $entry, '9.2' ),
		'Address (City)' => rgar( $entry, '9.3' ),
		'Address (State / Province)' => rgar( $entry, '9.4' ),
		'Address (ZIP/ Postal Code)' => rgar( $entry, '9.5' ),
		'Address (Cuntry)' => rgar( $entry, '9.6' ),
		'Donation Amount' => rgar( $entry, '15'),
		'Donation Type' => rgar( $entry, '16'),
		//'Donation Amount' => rgar( $entry, '1'),
		//'Donation Type' => rgar( $entry, '2'),
		'Hear more?' => rgar( $entry, '14.1'),
		'How did you hear about us?' => rgar( $entry, '11'), 
  );
// Create CSV File
// would be great if it had the submission id and the date in it
$fh = fopen('donation-entry.csv', 'w');

//Create headers
fputcsv($fh, $headers);

//Populate the data 
fputcsv($fh,$data);

//Close the file
fclose($fh);

}
add_action( 'gform_after_submission_3', 'populate_csv', 10, 2 );

// attach the same file to all User Notifications
add_filter("gform_notification_3", "add_attachment", 10, 3);
function add_attachment( $notification, $form, $entry ){
    // reference the notification by name
    if( $notification["name"] == "Test" ){
        // this is the file to be attached to all notifications
        $attachment = 'donation-entry.csv';
        $notification['attachments'] = array($attachment);
    }
    return $notification;
}
```

---

<div class="post-metadata">

**Author:** ![user603de2cfd8659270](https://avatars.discourse-cdn.com/v4/letter/u/e47c2d/32.png) [@user603de2cfd8659270](https://community.gravityforms.com/u/user603de2cfd8659270)\
**Post date:** [March 15, 2021, 1:51am UTC](https://community.gravityforms.com/t/how-to-attach-current-form-data-to-populate-csv-and-add-to-notifications-resolved/8315/2 "2021-03-15T01:51:47Z")

</div>

I managed to figure it out. Because I was using after form submission it was sending the notification before the CSV File was updated. so I had to change the  
` add_action( 'gform_after_submission_3', 'populate_csv', 10, 2 );`  
to  
` add_action( 'gform_pre_submission_3', 'populate_csv', 10, 2 );`

which allowed me to grab form data before notifications are sent. However because “gform\_pre\_submission\_3” is triggered before notifications are sent and entries are created, you cant use  
`rgar( $entry, '12' )`  
So I changed that to:  
`rgar( $_POST, 'input_12' )`

This solved my issue.

---

<div class="post-metadata">

**Author:** ![JamieO](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/jamieo/32/2521_2.png) [@JamieO](https://community.gravityforms.com/u/JamieO)\
**Post date:** [March 16, 2021, 3:59pm UTC](https://community.gravityforms.com/t/how-to-attach-current-form-data-to-populate-csv-and-add-to-notifications-resolved/8315/3 "2021-03-16T15:59:43Z")

</div>


