Current location: Hot Scripts Forums » Programming Languages » PHP » Form to Text File

Form to Text File

Reply
  #1 (permalink)  
Old 03-05-05, 03:43 AM
Justin171985 Justin171985 is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Form to Text File

Hello,

What I would like to accomplish is having a newsletter signup form on my site that has name, email, and subscribe/unsubscribe radio buttons... and I would like the contents that a visitor enters in this form to added to a text file for me to gain access to when I get ready to send a newsletter, b/c I have a desktop based mailing system I'd like to use, and I can just add and delete accordingly.

When someone Subscribes I would like the text file to read something like %Name%email, and have each on a seperate line.. and when one unsubscribes have this info deleted from the text file so there is no confusion.

If anyone could help me, I'd appreciate it,
Justin.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-05-05, 11:47 AM
STphp STphp is offline
Newbie Coder
 
Join Date: Nov 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Justin, I wrote a small tutorial on adding data to text files a while ago, I'll post it below:

-----------------------------------------------------------

This tutorial is to show you how to write data to txt files. We will do this by making a simple user registration form.

Let's make a page and call it register.php and this will have the sign up form on it.

Code:
<form name="form1" method="post" action="signup.php"> Username: <input type="text" name="user"><br>Email: <input type="text" name="mail"><br>Experience: <select name="exp"> <option value="beginner">Beginner</option> <option value="intermediate">Intermediate</option> <option value="advanced">Advanced</option> </select><br> <input type="submit" name="Submit" value="Sign Up"> </form>

Now, the actual php is going to be on the signup.php page. We will write to a file called users.txt as that is what we used when retrieving the data.

To do this, we will use the fopen() function, which opens a specified file and depending on the mode you enter as the second argument, will specify how you want to write to the file.

The modes are represented by letters and here is what each mode does:

r = Opens the file for reading only and places the file pointer at the beginning of the file.

r+ = Opens the file for reading and writing and places the file pointer at the beginning of the file.

w = Opens the file for writing only and places the file pointer at the beginning of the file. If the file does not exist, it attempts to create it.

w+ = Opens the file for reading and writing and places the file pointer at the beginning of the file. If the file does not exist, it attempts to create it.

a = Opens the file for writing only and places the file pointer at the end of the file. If the file does not exist, it will try to create it (What we will use).

a+ = Opens the file for reading and writing and places the file pointer at the end of the file. If the file does not exist, it will attempt to create it.


Now, we are going to set a couple variables to start with:

Code:
$username = $_POST['user'];
$email = $_POST['mail'];
$experience = $_POST['exp']; //the data
$data = "$username | $email | $experience\n";

//open the file and choose the mode
$fh = fopen("users.txt", "a");

Next step is to actually write to the file. To do this we will use the fwrite function. This function will read the $fh variable and put the $data string into it.


Code:
fwrite($fh, $data); //close the file fclose($fh); 
print "User Submitted";


Now you have done it, so just make sure your users.txt file has been CHMOD to 777 and your signup.php page should now look like this:

PHP Code:
<?php

$username 
$_POST['user'];
$email $_POST['mail'];
$experience $_POST['exp'];

//the data
$data "$username | $email | $experience\n";

//open the file and choose the mode
$fh fopen("users.txt""a");
fwrite($fh$data);

//close the file
fclose($fh);

print 
"User Submitted";

?>
There it is, you now know how to store data using txt files.
__________________
Shaun

Last edited by STphp; 03-05-05 at 11:53 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-05-05, 12:00 PM
STphp STphp is offline
Newbie Coder
 
Join Date: Nov 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
To delete a line, just make a form with a text field in where users submit their email address or something and call the field 'email' and use this to remove the line from the file:

PHP Code:
<?php

$Lines 
file("users.txt");
$cLines count($Lines);
$dlete $_POST['email'];

  foreach(
$Lines as $Key => $Val) { 

   
$Data[$Key] = explode("|"$Val);

    if ( 
trim($Data[$Key][1]) == $dlete ) {

     unset(
$Lines[$Key]);
     
$fp fopen("users.txt""w");
     
$fw fwrite($fpimplode(''$Lines));
     
fclose($fp);

    }

  }

print 
"Deleted";

?>
__________________
Shaun
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-05-05, 12:03 PM
STphp STphp is offline
Newbie Coder
 
Join Date: Nov 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
I hope all that helped. If you have any more questions concerning flat file databases, feel free to ask.
__________________
Shaun
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 03-07-05, 03:37 AM
Justin171985 Justin171985 is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
This has worked just great, I really appreciate your help with this.

Justin.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 03-07-05, 04:09 AM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
due to the way files are handled i suggest reading about flock() to avoid corruption/loss of data
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 03-08-05, 01:20 AM
eddyvlad eddyvlad is offline
Wannabe Coder
 
Join Date: Sep 2003
Location: In The Bloody Pits Of Hell
Posts: 156
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by STphp
To delete a line, just make a form with a text field in where users submit their email address or something and call the field 'email' and use this to remove the line from the file:

PHP Code:
<?php
 
 $Lines 
file("users.txt");
 
$cLines count($Lines);
 
$dlete $_POST['email'];
 
   foreach(
$Lines as $Key => $Val) { 
 
    
$Data[$Key] = explode("|"$Val);
 
     if ( 
trim($Data[$Key][1]) == $dlete ) {
 
      unset(
$Lines[$Key]);
      
$fp fopen("users.txt""w");
      
$fw fwrite($fpimplode(''$Lines));
      
fclose($fp);
 
     }
 
   }
 
 print 
"Deleted";
 
 
?>
What this script do is actually take all the data from the .txt file and put all the lines into arrays, delete the array value of that line and overwrite the file with all the data.
I doubt there's a way to delete the line directly from the file.
__________________
Mr. Brown Eyes
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 03-08-05, 05:44 AM
STphp STphp is offline
Newbie Coder
 
Join Date: Nov 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
it's still the shortest and simplest way to actually delete the said line from the file...
__________________
Shaun
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 11-09-06, 10:06 PM
tscott tscott is offline
New Member
 
Join Date: Nov 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Error

I get this when I try to use it,
Code:
Parse error: parse error, unexpected T_VARIABLE in /home/www/tscott.us/register2.php on line 6
http://tscott.us/register.php is where it is currently active, can somebody help, thanks
My code is
PHP Code:
<?php
$username 
$_POST['Username'];
$email $_POST['email'];
$password $_POST['password'];
$signature $_POST['sig']
$data "$username | $email | $password | $signature/n";
$fh fopen("users.txt""a");
fwrite($fh$data);
fclose($fh);
print 
"Succesfully Registered, you may now log in with your username and password"
}else{
  echo
"FAILED!";?>
~Tyler ~
Thanks ahead
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 11-10-06, 02:41 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 7,572
Thanks: 5
Thanked 27 Times in 24 Posts
There's a semicolon missing in line 5.

$signature = $_POST['sig'];
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
save form data to text file hinch JavaScript 1 12-10-04 05:22 AM
Form Results To Text File bxcheats PHP 2 09-25-04 08:36 PM
Form Results to an HTML or Text File bxcheats PHP 3 09-25-04 10:46 AM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 07:19 PM
Need to submit form to database and text file - Any ideas how? dpreiss ASP 1 08-21-03 06:02 PM


All times are GMT -5. The time now is 03:38 PM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.