Simple Script?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
K0D3R
Forum Newbie
Posts: 1
Joined: Mon Oct 22, 2007 1:57 pm

Simple Script?

Post 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
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

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