Solved

Need script to collect email addresses

Posted on 2004-03-26
1
397 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
ID: 10694959
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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Programatically edit Web.config 8 36
css border raidius issue 7 89
How to replace a token in a string with square brackets? 2 75
Grunt Copy file to another destination. 1 56
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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:

792 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