Solved

Need script to collect email addresses

Posted on 2004-03-26
1
391 Views
Last Modified: 2010-05-18
I send out a newsletter to subscribed customers each month.  I need a webpage that allow individuals to enter their email addresses they can start receiving the latest newsletter.  I would like the email addresses to be stored in a text file. Thanks.
0
Comment
Question by:mcdonald_g
1 Comment
 
LVL 6

Accepted Solution

by:
Reapz earned 125 total points
Comment Utility
Here's an example using PHP that checks the file to see if the email address is already listed and if not writes the email to a new line. If you want the file to be stored somewhere else then just change mailinglist.txt to path/to/mailinglist.txt. (The extension of this webpage must be .php)

<?php
if($_GET['signup'] == "true"){
    $errors = array();
    if($_POST['email'] <> ""){;      
        //If the email field is not empty we validate the email
        if (ereg("^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$", $_POST['email'])){
            //If it's ok we open your txt file and check to see if its already listed
            $exists = false;
            if(file_exists("mailinglist.txt")){
                $handle = fopen("mailinglist.txt","r") or die("Failed to open file!");
                while (!feof($handle)) {
                    $line = preg_replace("'\n'", "", fgets($handle));
                    if($line == $email){
                        $exists = true;
                        array_push($errors, "That email address is already in the mailing list!");
                    }
                }  
               fclose($handle);
            }
            //If it doesn't exist we reopen (or create) the file for writing
            if($exists == false){
                $handle = fopen("mailinglist.txt","a+");
                fwrite($handle, "$email\n");
                fclose($handle);
            }
        } else {
            array_push($errors, "That does not appear to be a valid email address!");
        }
    } else {
        array_push($errors, "You did not enter an email address!");
    }
}
?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php if($_GET['signup'] == "true" && count($errors) == 0){ ?>
You have successfully joined the mailing list!
<?php } elseif(count($errors) > 0){ ?>
The following errors occured...<br><br>
<?php foreach($errors as $e){
      echo "$e<br>";
} ?>
<br>Please click the back button on your browser to correct the mistakes...
<?php } else { ?>
<form name="form1" method="post" action="?signup=true">
<p>To receive regular updates and information enter your email address in the box below...</p>
<p><input name="email" type="text" id="email" size="30"></p>
<p><input type="submit" name="Submit" value="Join The Mailing List"></p>
</form>
<?php } ?>
</body>
</html>
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now