[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Need script to collect email addresses

Posted on 2004-03-26
1
Medium Priority
?
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 6

Accepted Solution

by:
Andy earned 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
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 …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

656 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