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!
PHP Forms Question
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
fopen(), fwrite() and fclose() for reference.
Basically, check if the form has been submitted
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.
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.
Basically, check if the form has been submitted
Code: Select all
if (!empty($_POST['submit'])) {
//...stuff goes here...
}Code: Select all
$file = 'emails.txt';
$handle = fopen($file,'a');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";
}