Writing Form Data to Text File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cojake76
    New Member
    • Mar 2010
    • 2

    Writing Form Data to Text File

    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.

    [FORM]
    [code=html]<html>
    <body>
    <form action="contact .php" method="post">
    Email: <input type="text" email="email" />
    <input type="submit" />
    </form>
    </body>
    </html>[/code]

    [PROCESS PAGE]
    [code=php]<?PHP
    $filename = "contact.tx t";
    $text = $_POST['email'];
    $fp = fopen ($filename, "a");
    if ($fp) {
    fwrite ($fp, $text);
    fclose ($fp);
    ?>[/code]

    Thanks,
    Jake
    Last edited by Atli; Mar 25 '10, 01:28 AM. Reason: Added [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    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.
    [code=php]// The "email" attribute here is invalid
    <input type="text" email="email" />

    // This should be
    <input type="text" name="email" />[/code]


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

    Comment

    • cojake76
      New Member
      • Mar 2010
      • 2

      #3
      Atli,

      Great Answer! I have it working.

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

      Thank you very much!

      Jake

      Comment

      Working...