View Single Post
  #1  
Old 04-06-2004, 10:02 AM
  Jon's Avatar 
Jon Jon is offline
 

X-Guru
  
Join Date: Oct 2002
Location: Vancouver, Canada
Posts: 4,200
 

Default Static Page for newsletter subscription or unsubscription

Here's the standard code. It posts to itself, so the page can be stand alone with your headers and/or footers, or you can just make a small box on your index page or something. Lots of uses, so you can be creative.

If you copy all this code, modify your database settings, and rename it newsletter.php and upload it, it should work for you.

Code:
<? #=================================# # CODE WRITTEN BY JON PETERS # # PROVIDED AS IS WITH NO WARRANTY # # I.E. USE AT YOUR OWN DESCRETION # #=================================# // Initialize variables $error = ""; $response = ""; #=================================# # CONFIG # #=================================# // Edit this with your own database information $sql_host ="localhost"; $sql_user ="YOUR_DATABASE_USER"; $sql_db ="YOUR_DATABASE"; $sql_password ="YOUR_DATABASE_PASSWORD"; // End Config #=================================# # FUNCTIONS # #=================================# function check_email($str) { //returns 1 if valid email, 0 if not if(ereg("^.+@.+\\..+$", $str)) { return 1; } else { return 0; } } #=================================# # ON POST COMMANDS # #=================================# if ($_POST['mode'] != "") { // Connect and Select a Database $link = mysql_connect($sql_host, $sql_user, $sql_password) or die ("Couldn't connect to MYSQL Database"); mysql_select_db($sql_db) or die("Could not select the database '" . $sql_db . "'. Are you sure it exists?"); // Format the email address $email = $_POST['email']; $email = strip_tags($email); $email = addslashes($email); // Check for valid email address if (check_email($email) != 1) { $error = Y; } if ($error != "") { $response = "ERROR: Invalid E-mail address!"; } // If there is not an error, we check the mode // Subscribe elseif ($_POST['mode'] == "subscribe") { $result = mysql_query("select email from xcart_maillist where email='$email' LIMIT 1"); $emailcheck = mysql_fetch_row($result); $emailcheck = $emailcheck[0]; mysql_free_result($result); if ($emailcheck != "") { $response = "ERROR: E-mail is already subscribed to the Newsletter."; } else { mysql_query("INSERT INTO xcart_maillist values('$email', now())"); $response = "ADDED: E-mail address successfully added to the Newsletter!"; } } // Unsubscribe elseif ($_POST['mode'] == "unsubscribe") { $result = mysql_query("select email from xcart_maillist where email='$email' LIMIT 1"); $emailcheck = mysql_fetch_row($result); $emailcheck = $emailcheck[0]; mysql_free_result($result); if ($emailcheck == "") { $response = "ERROR: E-mail address is not subscribed to the Newsletter."; } else { mysql_query("DELETE FROM xcart_maillist where email='$email' LIMIT 1"); $response = "REMOVED: E-Mail address has been successfully removed from the Newsletter."; } } } ?> <html> <head> <title>Newsletter Subscription</title> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> </head> <? if ($response != "") { echo "<font color=\"#FF0000\">$response</font>"; } ?> <form name="newsletter" action="newsletter.php" method="POST"> <center> <table border="0" width="250" cellpadding="2"> <tr> <td width="50%"><input name="email" size="15"></td> </tr> <tr> <td width="50%"> <input type="radio" value="subscribe" checked name="mode"> Subscribe <input type="radio" value="unsubscribe" name="mode"> Unsubscribe </td> </tr> <tr> <td width="50%"> <p align="center"><input type="Submit" value="Submit" name="Submit"></td> </tr> </table> </center> </form> </body> </html>
Reply With Quote