By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,400 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,400 IT Pros & Developers. It's quick & easy.

Writing Form Data to Text File

P: 2
Hi All,

I'm new to the forum and pretty green with PHP (SQL guy trying to learn).

Anyway, I decided to write a script that would allow people to sign up for a notification on my website. I want to put single field form on my page to collect the email address and dump that address into a test file on my server (adding each new address to that same file). I can't, however, for the life of me get the data to write to that file. I have checked all the CHMOD settings (set to 777 on both the form page and process page). Plus, I wrote another script to to write non form data to that same file- that worked. My scripts are below, any help would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <form action="contact.php" method="post">
  4. Email: <input type="text" email="email" />
  5. <input type="submit" />
  6. </form>
  7. </body>
  8. </html>
Expand|Select|Wrap|Line Numbers
  1. <?PHP
  2. $filename = "contact.txt"; 
  3. $text = $_POST['email'];
  4. $fp = fopen ($filename, "a"); 
  5. if ($fp) {
  6. fwrite ($fp, $text);
  7. fclose ($fp);
  8. ?>
Mar 25 '10 #1
Share this Question
Share on Google+
2 Replies

Expert 5K+
P: 5,058
Hey Jake.

I see two errors in there. The first one is in the <input> you use for the email address. The name of the input should be put in an attribute called "name". Your input uses an "email" attribute, which doesn't exists.
Expand|Select|Wrap|Line Numbers
  1. // The "email" attribute here is invalid
  2. <input type="text" email="email" />
  4. // This should be
  5. <input type="text" name="email" />

The second is in the PHP code. You are missing the closing } for the IF statement, which should cause a parse error. To fix that you just need to add a } before the closing ?> tag.

Also, the fwrite() function does not add a new-line char, or anything like that, between what it writes, so unless you want all the emails packed together in one long string, you might want to add a comma, or something, between them :)
Mar 25 '10 #2

P: 2

Great Answer! I have it working.

I'm going to add that comma and some style to my form.

Thank you very much!

Mar 25 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.