I'm trying to setup a PHP form on my site, which will just be a simple feedback form that sends the data the person submitted to a text file. I had originally had it in ASP, but I have found that my web server doesn't have a ASP client installed, so, no ASP.
I've only started learning PHP, so I don't really know how I could do it and things I've searched on google haven't really been helpful. Can anyone help me with this, as I don't really wanna register with a PHP site and post there (i'm too lazy)...
So far, all I've got is:
<?php $filename = "feedback.txt" ;?>
So you can see, I have hardly any idea as what else to do. I figured you'd need a way to open the file to write to it, and I found
fopen( ) ; would do that, but how would I write to the text file without removing anything that was previously there?
Quote from: Os à šltimo Quão Queijo ^_^ on Wed 12/05/2004 03:32:17So far, all I've got is:
<?phpÃ, Ã, $filename = "feedback.txt" ;?>
<?php $filename = "feedback.txt"; # WRITE # $f = fopen($filename,"w"); fwrite($f,"w00t"); fclose($f); # READ # $f = fopen($filename,"r"); fread($f,filesize($filename)); # reads the whole file fclose($f);?>
Whoops:
$data = fread($f,filesize($filename)); # reads the whole file
You can add $newstuff like this:
fwrite($f,$data.$newstuff); # '.' joins strings together
or
$f = fopen($filename,"a");
fwrite($f,$newstuff);
... and that "are" in there is a small R...
are... ;D
I wonder where that came from :P
So, where it says w00t... thats what goes into the file? So to have multiple lines and taking the info from the three fields which are Name, Email and Comment... would I do:
<?php $filename = "feedback.txt"; # WRITE # $f = fopen($filename,"w"); fwrite($f,"$Name"); fwrite($f,"$Email"); fwrite($f,""); fwrite($f,"$Comment"); fwrite($f,"========================"); fwrite($f,""); fclose($f); # READ # $f = fopen($filename,"are"); fread($f,filesize($filename)); # reads the whole file fclose($f);?>
So would that work?
edit: I found out why the r turns into are... auto txt remover thingy...
No, what you want is: (not colour coded because I'm lazy and it really serves no purpose anyway if you don't already understand what's going on)
<?
import_request_variables('gP','r_'); // imports request variables - you may or may not have to do this
$f=fopen('feedback.txt','a'); // opens feedback.txt for appending
fwrite($f,"$r_name\n$r_email\n\n$r_content\n=======================\n"); // adds stuff to the file, assuming the fields are called "name", "email" and "content"
fclose($f);
?>
It still doesn't work...
Heres my everything in my HTML file that deals with the form... maybe something wrong there... but I doubt it.
This is feedback.html...
<form action="feedback.php" method="post">
<input type="textarea" name="name" />
<input type="textarea" name="email" />
<textarea rows="5" cols="25" name="comments"></textarea>
<input type="submit" value="Submit Comment" />
<input type="reset" />
</form>
And then here is the php in the feedback.php:
<?phpimport_request_variables('gP','r_'); // imports request variables - you may or may not have to do this$f=fopen('feedback.txt','a'); // opens feedback.txt for appendingfwrite($f,"$r_name\n$r_email\n\n$r_content\n=======================\n"); // adds stuff to the file, assuming the fields are called "name", "email" and "content"fclose($f);?>
Should the php file be nothing BUT php, because I've got the regular HTML of my site which has the thank you comment and stuff there as well...
Quote from: Os à šltimo Quão Queijo Raspado De Macaco Burro ^_^ on Thu 13/05/2004 00:34:09Should the php file be nothing BUT php, because I've got the regular HTML of my site which has the thank you comment and stuff there as well...
Not if you don't want to.Ã, Just keep the HTML outside the <? ?>s.Ã, If you want to keep the two separate, you can insert
<?php require ("feedback.php"); ?>
when you need to, but it will effectively "insert" the whole PHP file.
Wouldn't that be include, not require? ???
I thought so too...
include and require do basically the same thing, but behave differently on failure. include produces a warning, require a fatal error.
D'oh, of cource... I had read that, but ofcource I didn't remember... require is better in this case then, I think too...