We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18608
    • 112 Posts
    If anyone could point me in direction of a solution to the following problem, I would be very grateful:

    I have a large form with multiple lines of data that I want to save to a .csv file and then attach that .csv file to email and send. Is there a way to do that with FormIt?
      Mathias Dannevang | Webdesigner at dannevang.org | Tweets @dannevang
    • Yes we can! smiley

      You can do this by using FormSave. It saves your form submissions in the database. You can export your saved data from within the FormSave manager component to CSV.

      More info here: http://rtfm.modx.com/display/ADDON/FormSave
        Sterc Internet & Marketing | MODX Founding Partner | Chairman of the MODX Advisory Board

        In need of a MODX consult? Try our MODX Developers Experts!
        • 18608
        • 112 Posts
        Yes, I considered FormSave, my problem is that it won't work with a manual process to export the data. I need the .csv file emailed automatically each time the form is submitted.
          Mathias Dannevang | Webdesigner at dannevang.org | Tweets @dannevang
        • You would need to write your own Hook to do this. You would have to save file to a directory then attach to email and send. If I get some free time I will see if I can get a hook coded up.
            • 44920
            • 48 Posts
            Quote from: MathiasD at Mar 25, 2013, 10:59 AM
            Yes, I considered FormSave, my problem is that it won't work with a manual process to export the data. I need the .csv file emailed automatically each time the form is submitted.

            Hi Mathias,

            I hit on the same need - to generate .csv file from submitted form and send it as an attachment. wondering have you found some solution since?

            Thank you in advance
            krajicek
              MODX Revolution 2.2.8-pl (advanced)

              Extras:
              archivist 1.2.4 | Articles 1.7.1 | Babel 2.2.5 | getpage 1.2.3 | getresources 1.6.0 | quip 2.3.3 | SimpleSearch 1.6.1 | taglister 1.1.7 | TinyMCE 4.3.3 | Wayfinder 2.3.3

              PHP Version 5.6.20-0
              MySQL Version 5.5.31
              • 44920
              • 48 Posts
              Just in case if somebody is looking for the same - here is my solution (based on info from other posts) - may need fine tuning & don't forget to filter your $formValues so it is just one-dimensional array otherwise you will end up with values like 'Array' (as a result of array to string conversion)

              CreateCsvAttachment snippet:
              $formit =& $hook->formit;
              $formValues = $hook->getValues();
              
              foreach ($formValues as $key => $value) {
              	$formFields[] = $key;
              	$formFieldsValues[] = $value;
              }
              
              $attachment_path = 'assets/components/form_csvs/';
              // create csv name from form field 'name' stripped from blank spaces and 
              // all the strange chars; timestamp added at the end of file name
              $csv_name1 = str_replace(' ', '_', $formValues['name']);
              $csv_name2 = str_replace('/[^A-Za-z]+/', '_', $csv_name1) . '_' . time();
              $fp = fopen($attachment_path . $csv_name2 . '.csv', 'a');
              
              fputcsv($fp, $formFields);
              fputcsv($fp, $formFieldsValues);
              
              fclose($fp);
              
              $hook->modx->getService('mail', 'mail.modPHPMailer');
              $hook->modx->mail->mailer->AddAttachment($attachment_path . $csv_name2 . '.csv');
              
              return true;
              


              and then in FormIt call is hook added before email hook
              [[!FormIt? &hooks=`CreateCsvAttachment,email,...` ...]
              


              where '...' means other attributes or values

              hope it helps somebody [ed. note: krajicek last edited this post 9 years, 11 months ago.]
                MODX Revolution 2.2.8-pl (advanced)

                Extras:
                archivist 1.2.4 | Articles 1.7.1 | Babel 2.2.5 | getpage 1.2.3 | getresources 1.6.0 | quip 2.3.3 | SimpleSearch 1.6.1 | taglister 1.1.7 | TinyMCE 4.3.3 | Wayfinder 2.3.3

                PHP Version 5.6.20-0
                MySQL Version 5.5.31