Solved

PHP form submission dumped into an .csv

Posted on 2007-03-29
11
631 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
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
 
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 50

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 50

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 50

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculate values in an array 18 35
Form Processing in PHP 11 32
Checking if varaible is empty 6 31
Configuring a checkbox in CSS and php 18 32
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 look for a specific file type in a local or remote server directory using PHP.

896 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

15 Experts available now in Live!

Get 1:1 Help Now