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
<br><hr><br>
Name: $name <br>
Email: $email <br>
Comments: $comments <br>
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.<br>
It not, click <a href="http://www.wainscottyouthclub.org.uk" target="_self">here</a>.
</BODY>
</HTML>
EOD;
echo"$theResults";

?>

Any help greatly appreciated!

peter_budo commented: I like to see new member using code tags from first post +10

Recommended Answers

All 8 Replies

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
<br><hr><br>
Name: $name <br>
Email: $email <br>
Comments: $comments <br>
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.<br>
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...
}

}

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. :)

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;

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

Thank you!

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.

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');

?>

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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.