?
Solved

PHP form submission dumped into an .csv

Posted on 2007-03-29
11
Medium Priority
?
639 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 51

Accepted Solution

by:
Steve Bink earned 2000 total points
ID: 18818953
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
ID: 18818964
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
ID: 18818969
<?php

$data = $_POST;

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

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


?>
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 24

Expert Comment

by:glcummins
ID: 18818974
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 51

Expert Comment

by:Steve Bink
ID: 18819372
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
 
LVL 51

Expert Comment

by:Steve Bink
ID: 18819383
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
ID: 18819406
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
ID: 18819487
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
ID: 18819546
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 51

Expert Comment

by:Steve Bink
ID: 18819599
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
ID: 18820115
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
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 …
Suggested Courses

770 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