Welcome to WebmasterWorld Guest from 107.20.5.156

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

form to text file with php

     

astounded

2:13 am on Aug 22, 2002 (gmt 0)

10+ Year Member



I have very little php experience. I have looked extensively on the web for a simple php script that will take form results and put them in a text file. But I can't find one that isn't extrenely complicated. Does anyone have a suggestion. I don't need to email anything or check if someone filled out a form right. In fact, I don't want those two features. I just gather the script to take the results when someone submits a form, and put in a .csv or .txt file.

Sounds simple enough, but I haven't found much out there.

jatar_k

2:27 am on Aug 22, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



If it hasn't previously been offered

Welcome to WebmasterWorld astounded,

What you are asking is simple enough.

You have a file on the site somewhere and you open it with fopen() [php.net], you then put your comma delimited string together and then use fwrite() [php.net] to put the new line in the file and then fclose() [php.net] the file.

something like this

$writestring = "\"" . $name . "\",\"" . $email . "\"\r\n"; //double quotes around values escaped and return and newline chars at end
$filepointer = fopen("/dir/file.csv","a"); //appends to end of file
fwrite($filepointer,$writestring);
fclose($filepointer);

astounded

2:55 am on Aug 22, 2002 (gmt 0)

10+ Year Member



thanks jtar,

Can I stick this script snippet in a formmail script I am already using. I am using Jack's formmail for a simple email subscription list. But the form I want your snippet for has about 30 questions on it. Can I just stick this in Jack's formmail script and then point the form to it? If so, do I use your entire snippet or just part of it?

jatar_k

3:20 am on Aug 22, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



you can't actually stick it straight in, it needs to be customized to your script. I will try to explain a little more in depth, you will have to put it together yourself.

$writestring = "\"" . $name . "\",\"" . $email . "\"\r\n";

This line takes every variable submitted by your form and then puts it together in one long string with quotes around every value and a comma in between. It the adds a return (\r) and a newline character (\n). You will have to take all of the variables that are submitted with your form and add them to the string.

$filepointer = fopen("/dir/file.csv","a");

This creates a filepointer to a file which you will have to put somewhere and then put the real path to the file in instead of /dir/file.csv. The "a" denotes that the file is opened for append with the file pointer already set to the end of the file.

fwrite($filepointer,$writestring);

This takes the string that we constructed with all of the variables from the form and writes it to the end of the file.

fclose($filepointer);

We then close the file. You can add this straight into the script but you will have to make the adjustments that I mentioned. Otherwise it will not work.

The links in my first post go to php.net. It is a great php language reference. You may have to familiarize yourself with some basics to add this to the formmail script.

jatar_k

3:53 am on Aug 22, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I found an old script that does exactly what you want. It does a few variables to see if they are empty as well. I removed a massive portion of it, since I didn't want to confuse you too badly.

$info = "";
$info .= "\"$homeowner\",";
$info .= "\"$name\",";
$info .= "\"$soc1\",";
if (!empty($cobwrfull))
{
$info .= "\"$cobwrfull\",";
$info .= "\"$soc2\",";
} else {
$info .= "\"\",\"\",";
}
if (!empty($pmtreq))
{
$info .= "\"$pmtreq\",";
} else {
$info .= "\"\",";
}
$info .= "\"$company\",";
$info .= "\"$yrsempbwr\",";
$info .= "\"$cashout\",";
$info .= "\"$Calltime\",";
$info .= "\"$newNP\"";
$info .= "\r\n";


$responseList = "$DOCUMENT_ROOT/formfiles/response.dlm";
$filePointer = fopen($responseList,'a');
fwrite($filePointer, $info);
fclose($filePointer);

astounded

4:52 am on Aug 22, 2002 (gmt 0)

10+ Year Member



thanks much, I'll start learning

jatar_k

5:46 am on Aug 22, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Anytime you need a little help you know where to find us. ;)