954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Send form results to a .txt file

Hi, im making the website for a local youth club, and the leader there, wants to have comments posted automatically, under the contact box.

I'm fine with the contact form, that works fine. Its the same one used on my website, its been tested by 3 other people to. The redirection works fine too.

I want to know how to send the results to a .txt file, so I can include them under the form.

<?php

/* Subject and Email variables */

	$emailSubject = 'Online Contact Form';
	$webMaster = 'info@pauldavis.org.uk';

/* Gathering data variables */

	$name = $_POST['name'];
	$email = $_POST['email'];
	$comments = $_POST['comments'];
	
	$body = <<<EOD
<hr>
Name: $name 
Email: $email 
Comments: $comments 
EOD;
s
	$headers = "From: $email\r\n";
	$headers .= "Content-type: text/html\r\n";
	$success = mail($webMaster, $emailSubject, $body, $headers);
	
/* Results renderes as HTML */

	$theResults = <<<EOD
<html>
<head>
<title>Your Page Title</title>
<meta http-equiv="REFRESH"content="0;url=http://www.wainscottyouthclub.org.uk"></HEAD>
<BODY>
Your message was sent successfully. You should be automaticly redirected to the home page.
It not, click <a href="http://www.wainscottyouthclub.org.uk" target="_self">here</a>.
</BODY>
</HTML>
EOD;
echo"$theResults";

?>


Any help greatly appreciated!

pauldavis
Newbie Poster
7 posts since Aug 2008
Reputation Points: 20
Solved Threads: 0
 

Hi, im making the website for a local youth club, and the leader there, wants to have comments posted automatically, under the contact box.

I'm fine with the contact form, that works fine. Its the same one used on my website, its been tested by 3 other people to. The redirection works fine too. I want to know how to send the results to a .txt file, so I can include them under the form.

<?php

/* Subject and Email variables */

	$emailSubject = 'Online Contact Form';
	$webMaster = 'info@pauldavis.org.uk';

/* Gathering data variables */

	$name = $_POST['name'];
	$email = $_POST['email'];
	$comments = $_POST['comments'];
	
	$body = <<<EOD
<hr>
Name: $name 
Email: $email 
Comments: $comments 
EOD;
s
	$headers = "From: $email\r\n";
	$headers .= "Content-type: text/html\r\n";
	$success = mail($webMaster, $emailSubject, $body, $headers);
	
/* Results renderes as HTML */

	$theResults = <<<EOD
<html>
<head>
<title>Your Page Title</title>
<meta http-equiv="REFRESH"content="0;url=http://www.wainscottyouthclub.org.uk"></HEAD>
<BODY>
Your message was sent successfully. You should be automaticly redirected to the home page.
It not, click <a href="http://www.wainscottyouthclub.org.uk" target="_self">here</a>.
</BODY>
</HTML>
EOD;
echo"$theResults";

?>

Any help greatly appreciated!

Is the results what the user entered into the contact form?

Anyway, to read and write to files see:

fopen - http://www.php.net/manual/en/function.fopen.php
fwrite - http://www.php.net/manual/en/function.fwrite.php
fread - http://www.php.net/manual/en/function.fread.php

You can also use the "shortcuts":

file_get_contents() and file_put_contents(). The latter being only available in PHP5 but you can easily emulate it in PHP4.

eg:

if (!function_exists('file_put_contents')) {

function file_put_contents($file, $content, $flags = null, $context = null) {
// code here... etc...
}

}
digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 
Is the results what the user entered into the contact form?

Yes, I should have said that.

And thanks for the links, I did find a script with them in, but it didnt work. :)

pauldavis
Newbie Poster
7 posts since Aug 2008
Reputation Points: 20
Solved Threads: 0
 

Yes, I should have said that.

And thanks for the links, I did find a script with them in, but it didnt work. :)

Here is a basic example of writing the $body to a file. It will append new writes to the file.

$fp = fopen('file.txt', 'a');
if ($fp) {
   fwrite($fp, $body, strlen($body));
   fclose($fp);
} else {
   echo 'Error: Could not open file for writing';
}


When you read:

$content = file_get_contents('file.txt');
echo $content;
digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

You know what? Perfection! It works great, first time.

Thank you!

pauldavis
Newbie Poster
7 posts since Aug 2008
Reputation Points: 20
Solved Threads: 0
 

When you read:

$content = file_get_contents('file.txt');
echo $content;

But how would I implement this? I added it into the html for the page, with <?PHP and ?> tags, but nothing.

pauldavis
Newbie Poster
7 posts since Aug 2008
Reputation Points: 20
Solved Threads: 0
 

It should output whatever is in the file "file.txt".

So you have to submit the form at least once, to get an entry written to the file.

file_get_contents() is available PHP 4.3+ I think.

Is your version higher? Do you have errror reporting on?

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

?>
digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

No errors, as far as I can tell. And my host uses PHP 4 and 5.

pauldavis
Newbie Poster
7 posts since Aug 2008
Reputation Points: 20
Solved Threads: 0
 
No errors, as far as I can tell. And my host uses PHP 4 and 5.

Did you check to see if the file was actually written to? Does it have content?

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You