Solved

PHP form submission dumped into an .csv

Posted on 2007-03-29
11
628 Views
Last Modified: 2012-08-14
Hi,

I'm trying to get the results of a PHP form submission dumped into an .csv for import to ACT.

Does anyone know of a tutorial that can instruct me how to execute the above?

Thank you-
0
Comment
Question by:pmagony
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 50

Accepted Solution

by:
Steve Bink earned 500 total points
Comment Utility
The PHP manual hosts several good examples for reading and writing files.  Check this link:

http://www.php.net/manual/en/ref.filesystem.php

Otherwise, CSV files are just text files with a particular format.  
0
 
LVL 2

Expert Comment

by:wakemup
Comment Utility
It depends alot on what the data submitted to the form looks like. You may get away with something as simple as 'implode(',', $_POST);' (though you may have to pre-process incase people have put comma's intot he posted data...
0
 
LVL 4

Expert Comment

by:Mr-Ene
Comment Utility
<?php

$data = $_POST;

foreach($data as $csvdata) {
 $tosave .= $csvdata . ";";
}

file_put_contents('data.csv', $tosave);


?>
0
 
LVL 24

Expert Comment

by:glcummins
Comment Utility
Here is a brief example. It does not check the input, but you can do that in the foreach() loop as needed:

<?php

$output = "";

foreach ($_POST as $value)
{
      $output .= "$value,";
}

// After the loop is finished, cut off the last comma and add a newline
$output = substr($output, 0, -1) . "\n";

$filename = "/path/to/your/file.csv";

$filehandle = fopen($filename, 'a') or die("Cannot open file $filename");

fwrite($filehandle, $output);

fclose($filehandle);

?>
0
 
LVL 50

Expert Comment

by:Steve Bink
Comment Utility
Those solutions do not account for proper positioning in your CSV....they just write the $_POST willy-nilly and kind of hope the data is in the right order.  I've never seen any guarantee that field1 will always come before field2.

To use this kind of method, try changing it just a little.  Create an array of all the fields you want to be exported, then cycle through that array to retrieve the values.  This allows to create a specific structure for ACT's import.

<?
$myfields = ('Name','Phone','Address');
$output = '';
foreach ($myfields as $val) {
  $output .= "$val,";
}
$output = substr($output,0,-1) . "\n";

//NOW write the file
?>
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 50

Expert Comment

by:Steve Bink
Comment Utility
DOH!!  That code should have been:

<?
$myfields = ('Name','Phone','Address');
$output = '';
foreach ($myfields as $val) {
  $output .= $_POST[$val] . ',';
}
$output = substr($output,0,-1) . "\n";

//NOW write the file
?>
0
 
LVL 9

Author Comment

by:pmagony
Comment Utility
The scenario is as follows:

User completes form, hits submit.  Confirmation page captures data, sends email notification that the form has been submitted and includes the excel attachment in the email.

Routinet:

I'm listening to your post as I want to make sure I am doing this properly.  Assuming the above is utilized, will the user be prompted to save the output after submit? If so, this is not the case... this .csv file is for the recipient of the form results to import into ACT.

I am taking other posts into consideration with Routinet...  thanks-
0
 
LVL 2

Expert Comment

by:wakemup
Comment Utility
Once you've captured the data send the output back to the user with the following headers first:

header("Content-type: application/vnd.ms-excel");
header("Content-disposition:  attachment; filename=data.csv");

They will be prompted to save 'data.csv' (or open with a program depending on their browser).

0
 
LVL 9

Author Comment

by:pmagony
Comment Utility
WakemUp:

Thanks, but you mis-read my post.

"Assuming the above is utilized, will the user be prompted to save the output after submit? If so, this is not the case... "
0
 
LVL 50

Expert Comment

by:Steve Bink
Comment Utility
No, the user will not be asked to save the output unless you make it so.  Take a look at the posts by glcummins or Mr-Ene for the functions you will want for writing the file.  You can find information on those (and other) file functions at the link in my first post:

http://www.php.net/manual/en/ref.filesystem.php

0
 
LVL 29

Expert Comment

by:rdivilbiss
Comment Utility
After you've written the csv file to your web server, then you'll need to add headers to your email:

Best to use PEAR::Mail other wise you are going to have to manually write the correct headers then stream the file with the message.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now