Page 1 of 1

PHP Forms Question

Posted: Thu Jun 16, 2005 2:44 pm
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!

Posted: Thu Jun 16, 2005 3:11 pm
by hawleyjr
Any reason why you want to store this data to a text file and not a database?

Posted: Thu Jun 16, 2005 4:45 pm
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";
}