Page 1 of 1
Simple Script?
Posted: Mon Oct 22, 2007 2:04 pm
by K0D3R
It's been a LONG time since i've messed with php, but i remember some of the basics... For some reason i can't seem to find any information on how to create a php form that saves the input to an excel file... I've tried a few file modification methods, but it corrupts the file and can't be read anymore... I know there's GOT to be a simple way to acheive this!
Any help is greatly appreciated!
-K0D3R
Posted: Mon Oct 22, 2007 4:11 pm
by gregwhitworth
Code: Select all
<?php
header ("Location: http://www.karenwhitworth.net/thankyou.htm");
//Contact form field variable settings.
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$msg = $_POST['message'];
$newsletter = $_POST['newsletter'];
$cr = "\n";
$data = $email .$cr;
$file = "newsletter.csv";
//newsletter setting
if (!$file_handle = fopen($file, "a"))
{echo "Cannot open file";
}
if (!fwrite($file_handle, $data)) {
echo "Cannot write to file";}
echo "You have successfully written data to $file";
fclose($file_handle);
//Configurations for email
$to = "";
$subject = "Form Response at Karen Whitworth.net";
$body = "From: $name <$email>\n\n";
$body .= "$msg";
mail($to, $subject, $body, "From: $email");
?>
This is actually really easy, and yet it is so hard to find. I'll explain:
Code: Select all
//newsletter setting
if (!$file_handle = fopen($file, "a"))
{echo "Cannot open file";
}
if (!fwrite($file_handle, $data)) {
echo "Cannot write to file";}
echo "You have successfully written data to $file";
fclose($file_handle);
This says, if there is a file called newsletter.csv then just append $data (email in this case) to the bottom of it, if it isn't there, then create it - then append the $data.
That's it, I hope this helps.
--
Greg