Anybody know PHP? (woohoo, working now! thanks guys!)

Started by LordHart, Wed 12/05/2004 03:32:17

Previous topic - Next topic

LordHart

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:

Code: ags
<?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?

AJA@School

Quote from: Os àšltimo Quão Queijo ^_^ on Wed 12/05/2004 03:32:17So far, all I've got is:

Code: ags
<?php
Ã,  Ã, $filename = "feedback.txt" ;
?>


Code: ags

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

AJA@school

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

Ishmael

... and that "are" in there is a small R...
I used to make games but then I took an IRC in the knee.

<Calin> Ishmael looks awesome all the time
\( Ö)/ ¬(Ö ) | Ja minähän en keskellä kirkasta päivää lähden minnekään juoksentelemaan ilman housuja.

AJA

are...  ;D
I wonder where that came from :P

LordHart

#5
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:

Code: ags
<?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...

Fuzzpilz

#6
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)

Code: ags

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

LordHart

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

Code: ags

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

Code: ags

<?php
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);
?>


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

Migs

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

Code: ags
<?php require ("feedback.php"); ?>


when you need to, but it will effectively "insert" the whole PHP file.
This signature intentionally left blank.

LordHart


Ishmael

I used to make games but then I took an IRC in the knee.

<Calin> Ishmael looks awesome all the time
\( Ö)/ ¬(Ö ) | Ja minähän en keskellä kirkasta päivää lähden minnekään juoksentelemaan ilman housuja.

Fuzzpilz

include and require do basically the same thing, but behave differently on failure. include produces a warning, require a fatal error.

Ishmael

D'oh, of cource... I had read that, but ofcource I didn't remember... require is better in this case then, I think too...
I used to make games but then I took an IRC in the knee.

<Calin> Ishmael looks awesome all the time
\( Ö)/ ¬(Ö ) | Ja minähän en keskellä kirkasta päivää lähden minnekään juoksentelemaan ilman housuja.

SMF spam blocked by CleanTalk