<?php
//load configuration
require("config.php");
//print header
echo $header;
?>
<h1><?php echo $title; ?></h1>
<hr>
<?php
//form submitted
if(isset($_POST["submit"])){
//connect to database
@mysql_connect($db_server,$db_user,$db_password) or die("Database server connection failed. Check variables \$db_server, \$db_user and \$db_password in config.php");
@mysql_select_db($db_name) or die("Selecting database failed. Check variable \$db_name in config.php");
$name = $_POST["name"];
$email = $_POST["email"];
//subscribe
if($_POST["action"]=="subscribe"){
//check if name is long enough
if(strlen($name)<3){
?><p>Name must consist of at least 3 characters.</p><?php
//check if email is valid
}else if(!eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,3}$",$email)){
?><p>Email address is not valid.</p><?php
//check if email already exists
}else if(@mysql_num_rows(@mysql_query("SELECT id FROM $db_table WHERE email='$email';"))){
?><p>Email address exists already.</p><?php
//insert values
}else{
@mysql_query("INSERT INTO $db_table (email,name) VALUES ('$email','$name');");
//error occurred
if(@mysql_error()){
?><p>An unknown error occurred. Please try again later.</p><?php
//successful
}else{
?><p>Subscription was successful. Thank you!</p><?php
}
}
//unsubscribe
}else{
@mysql_query("DELETE FROM $db_table WHERE email='$email';");
//error occurred
if(@mysql_error()){
?><p>An unknown error occurred. Please try again later.</p><?php
//email not found
}else if(@mysql_affected_rows()==0){
?><p>Email address not found.</p><?php
//successful
}else{
?><p>You have unsubscribed successfully!</p><?php
}
}
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table>
<tr>
<td>Your name:</td>
<td><input type="text" name="name" value="" maxlength="255"></td>
</tr>
<tr>
<td>Your email address:</td>
<td><input type="text" name="email" value="" maxlength="255"></td>
</tr>
<tr>
<td> </td>
<td><input type="radio" name="action" value="subscribe" id="action_subscribe" checked> <label for="action_subscribe">subscribe</label> <input type="radio" name="action" value="unsubscribe" id="action_unsubscribe"> <label for="action_unsubscribe">unsubscribe</label></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
<p align="right"><a href="admin.php">Administration</a></p>
<?php
//print footer
echo $footer;
//close database connection
@mysql_close();
?>