PHP Forms Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bad959fl
Forum Newbie
Posts: 14
Joined: Thu Jun 16, 2005 2:26 pm

PHP Forms Question

Post by bad959fl »

Hello,

I'm currently using a basic formmail.pl script for the forms on my website. I have a newsletter signup option on all of my forms for visitors to subscribe. Up until now, I have been importing the email addresses and names into a text file for my mailing list. I basically copy and paste each entry. Since the newsletter is reaching 2,000 entries and the signup have been increasing every day, this is becoming a major burden on managing.

I would like to get some assistance for creating a PHP form which will do all of these tasks:

1. Only capture email address and first name for visitors who check the newsletter signup box.
2. Output the visitors email address and name into a flat file on the server.
3. Format the email and name like this:

"anyone1@email.com","Name"
"anyone2@email.com","Name"
"anyone3@email.com","Name"

4. Check and make sure any duplicate email addresses will not be added to the text file.

You can review one of my current forms here:

http://www.shapefit.com/newsletter.html

Any help would be greatly appreciated.

Thanks!
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Any reason why you want to store this data to a text file and not a database?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

fopen(), fwrite() and fclose() for reference.

Basically, check if the form has been submitted

Code: Select all

if (!empty($_POST['submit'])) {
//...stuff goes here...

}
Assuming you have a submit button. Now you have to open the file with the a modifier which will open the file for writting only at the end of the file.

Code: Select all

$file = 'emails.txt';
$handle = fopen($file,'a');
Now you want to add to the file. First we should check if we have permission set properly. If we cannot open the file, terminate the script.

Code: Select all

if (is_writable($file)) {
   //assuming your input field is called email
   if (fwrite($handle, $_POST['email']) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
   
   echo "Success, wrote ($_POST['email']) to file ($file)";
   
   fclose($handle);

} else {
   echo "The file $file is not writable";
}
Post Reply