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
Solved

PHP form submission dumped into an .csv

Posted on 2007-03-29
11
633 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 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