Forum Moderators: coopster
Sounds simple enough, but I haven't found much out there.
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);
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?
$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.
$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);