Solved

PHP form submission dumped into an .csv

Posted on 2007-03-29
11
636 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 …

739 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