Form to txt file

tony84

Free Member
Apr 14, 2008
6,624
1
1,414
Manchester
Hi,

Im looking for a script that would allow me to write 2 fields in a form to a text file...and every time that form is completed it adds the contents to the file - so effectively it builds up a list.

Does anyone know of any scripts i can use?

Cheers
Tony
 

tony84

Free Member
Apr 14, 2008
6,624
1
1,414
Manchester
I dont need that. Its basically just to get names and email addresses for anyone interested in signing up for a newsletter.

Ill then copy across the details to a CSV file i have.

To be honest it could just be an email form to email the details over to me or anything to record them. Im not expecting that many so something simple is fine.
 
Upvote 0

tony84

Free Member
Apr 14, 2008
6,624
1
1,414
Manchester
It will be. I have a list of leads ive bought over the last year but not got a newsletter system in place just yet. So i was going to add any to that.

i dont really want my csv file online - sites get hacked all the time so i need to ensure clients information is secure.
 
Upvote 0

lynxus

Free Member
  • Business Listing
    Jul 5, 2011
    1,343
    316
    Gloucester, UK
    imsupporting.com
    Sorry, its a quick 2second writeup..

    Something like this will work.


    form:
    HTML:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Form</title>
    </head>
    
    <body>
    <form id="form1" name="form1" method="post" action="script.php">
      <p>name: 
        <label>
          <input type="text" name="name" id="name" />
        </label>
      </p>
      <p>email: 
        <label>
          <input type="text" name="email" id="email" />
        </label>
      </p>
      <p>
        <label>
          <input type="submit" name="button" id="button" value="Submit" />
        </label>
      </p>
    </form>
    </body>
    </html>
    the script that the form calls ( script.php )
    PHP:
    <?php
    // the fileyou want your emals n stuff in
    $myFile = "saveddata.txt";
    $name = $_POST['name']; // the name field in the html <input type="text" name="name" id="name" />
    $email = $_POST['email']; // The email field <input type="text" name="email" id="email" />
    
    
    // magicc happens here
    $fh = fopen($myFile, 'a') or die("can't open file"); // Open file in "a" append mode.
    $stringData = "Name= $name , Email= $email \n"; // Create the data that we will put in the file. ( \n means "newline" )
    fwrite($fh, $stringData); // Write the data  
    fclose($fh); // close the file.
    
    echo "Thanks, Stuff has been added to our system"; // print to the page that its done
    
    
    ?>
     
    Last edited:
    • Like
    Reactions: tony84
    Upvote 0

    SimplyWebsites

    Free Member
    Nov 26, 2011
    51
    6
    Leicester
    You say this should be secure, well writing to a text file is certainly not secure

    If you write to a text file then if they can find where it is a hosted then they can download all of your customers details.

    If you want it to be secure then you should use a database or make sure the text file is not in a publicly accessible directory.
     
    Upvote 0
    You say this should be secure, well writing to a text file is certainly not secure

    If you write to a text file then if they can find where it is a hosted then they can download all of your customers details.

    If you want it to be secure then you should use a database or make sure the text file is not in a publicly accessible directory.

    It would be quite simple to only give the server FTP user read permissions and not allow the public any permissions with CHMOD.
     
    Upvote 0

    tony84

    Free Member
    Apr 14, 2008
    6,624
    1
    1,414
    Manchester
    It would be quite simple to only give the server FTP user read permissions and not allow the public any permissions with CHMOD.

    Done that.

    To be honest security is an issue, i thought flat files were more secure than databases :S
    Either way its an email and name, im not expecting many sign ups at all and ill be checking it daily so it wont be in there too long.
     
    Upvote 0

    Posilan

    Free Member
    Dec 20, 2010
    2,540
    878
    Manchester
    The problem with a text file is if someone finds the path it will simply download. How about using a file with a .php extension and head the file with a die command and open comment.

    That would stop it being visible if the path got found.

    Code:
    <?php
    die;
    /* EMAIL LIST
    A safer way would be to locate the text file above the public_html folder (eg on /home/USERNAME/myfile.txt instead of /home/USERNAME/public_html/myfile.txt) - that way only server side scripts should be able to access it.

    Steve
     
    Upvote 0
    A safer way would be to locate the text file above the public_html folder (eg on /home/USERNAME/myfile.txt instead of /home/USERNAME/public_html/myfile.txt) - that way only server side scripts should be able to access it.

    Steve

    Very true, if you have the ability to store files above the public_html folder it is much more secure. In my experience if using a basic shared hosting plan the chances of this being available are 50/50 but certainly worth checking.
     
    Upvote 0

    lynxus

    Free Member
  • Business Listing
    Jul 5, 2011
    1,343
    316
    Gloucester, UK
    imsupporting.com
    Sorry, its a quick 2second writeup..

    Something like this will work.


    form:
    HTML:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Form</title>
    </head>
    
    <body>
    <form id="form1" name="form1" method="post" action="script.php">
      <p>name: 
        <label>
          <input type="text" name="name" id="name" />
        </label>
      </p>
      <p>email: 
        <label>
          <input type="text" name="email" id="email" />
        </label>
      </p>
      <p>
        <label>
          <input type="submit" name="button" id="button" value="Submit" />
        </label>
      </p>
    </form>
    </body>
    </html>
    the script that the form calls ( script.php )
    PHP:
    <?php
    // the fileyou want your emals n stuff in
    $myFile = "saveddata.txt";
    $name = $_POST['name']; // the name field in the html <input type="text" name="name" id="name" />
    $email = $_POST['email']; // The email field <input type="text" name="email" id="email" />
    $mydate = date("F j, Y, g:i a");  
    
    // magicc happens here
    $fh = fopen($myFile, 'a') or die("can't open file"); // Open file in "a" append mode.
    $stringData = "Name= $name , Email= $email , When= $mydate \n"; // Create the data that we will put in the file. ( \n means "newline" )
    fwrite($fh, $stringData); // Write the data  
    fclose($fh); // close the file.
    
    echo "Thanks, Stuff has been added to our system"; // print to the page that its done
    
    
    ?>

    Updated with a datestamp..
     
    Upvote 0

    Latest Articles