help with PHP to csv (N00B Alert!)

Associate
Joined
3 Nov 2013
Posts
320
Location
Surrey
I have a HTML code that opens a simple web form where a user can nominate another member of staff to be entered into a prize draw, which is working lovely... See the screenshot below. (also the top name box is automatically filled in with the user who is logged on's name, to stop people nominating themselves!)

[url=http://postimg.org/image/9o7wbk94f/][/URL]

My question is that I'm trying to create a PHP script that will write a csv of all the data that is entered, so we can then export this into Excel.

So far I have "data.csv" and "write_to_csv.php" in the same folder, can anyone have a look at my code and see where I'm going wrong?

Code:
<html>
<body>

<?php
// Contact From
$fromstaff = $_POST['from'];
// Contact Student Name
$staffname = $_POST['staffname'];
// Contact Issue
$reason = $_POST['reason'];

//Mail To
$to = '[email protected]';
//Mail Subject
$subject = 'Give Thanks'; 
//Mail Message
$message = "
<html>
<head>
</head>
<body style=\"font-family:calibri\">
<h1><strong>GiveThanks Form <br /></strong></h1>

<p><strong>From</strong><br />
$fromstaff</p>

<p><strong>Staff Name</strong> <br />
$staffname</p>

<p><strong>Reason</strong> <br />
$reason</p>

</body>
</html>
"; 

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


$headers .= 'To: Charlie <[email protected]>' . "\r\n";
$headers .= 'From: Give Thanks <[email protected]>' . "\r\n";

$send_contact=mail($to,$subject,$message,$headers);


if($send_contact){
echo "
<html><head></head><body style=\"font-family:calibri\">
We've received your Give Thanks! Your nominee will now be entered into the draw
</body></html>
";
}
else {
echo "ERROR";
}

$fp = fopen ("/w2k8r2app1/D$/inetpub/GiveThanksTest/data.csv", "a+");
fwrite ($fp, "," . $_POST['from'] . "," . $_POST['staffname'] . "," . $_POST['reason'] . "," . "\a+");
fclose ($fp);
?>

</body>
</html>

Please go gently on me if it's a complete non-issue, have little experience with this!

Cheers :)
 
Associate
Joined
13 Aug 2008
Posts
221
What's the error? Or what is/isn't happening?

The link that Phunky provided seems to be exactly what you're asking?
 
Back
Top Bottom