I hope that this can help you out. this does everything you want, you will have to adjust it to fit your needs. first, it assumes that you have a database set up with fields for the id and email address.
<?
// First function connects to your database
function connect() {
$host = "localhost";
$username = "username";
$password="password";
$dbname = "dbname";
$link = mysql_connect ($host, $username, $password);
if ($link) {
mysql_select_db($dbname,$link);
}
return $link;
}
//next, you need to create functions to test the data
function filled_out($form_vars)
{
// test that each variable has a value
foreach ($form_vars as $key => $value)
{
if (!isset($key) || ($value == ""))
return false;
}
return true;
}
function valid_email($address)
{
// check an email address is possibly valid
if (ereg("[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$", $address))
return true;
else
return false;
}
$link=connect();
if ($submit) {
//if the form is submitted, test the data
if (!filled_out($HTTP_POST_VARS)) {//if the form is not filled out
$error=$error."<tr><td>You must fill out <i>ALL</i> Fields!</td></tr>";// I use this variable to hold all the errors
$i++;// count the error
}
if (!valid_email($email )) {//if the email is not valid
$error=$error." <tr><td> Email Address: Please enter a valid email address!</td></tr>";//add to error message
$email ="";//empty the email variable
$i++;//count error
}
if ($i<1) {//if there are no errors
$query="INSERT in table values ('0', '$email ')"; // I always use a unique id hence the 0
if (mysql_query($query, $link)) {
$body="$email has completed this form";
$subject="Form Submitted";
$mailfrom=$email ;
$mailto="YourEmail@url.com";
if(mail($mailto, $subject, $body, "From:$mailfrom")) {
header("location:thankyou.php");
}
} else {
//data could not be stored in database, send the email anyway
$body="$email has completed this form but there was an error and could not be added to the database";
$subject="Form Submitted";
$mailfrom=$email ;
$mailto="YourEmail@url.com";
if(mail($mailto, $subject, $body, "From:$mailfrom")) {
header("location:thankyou.php");
} else {
print ("your data was not received, please contact the system admin");
}
}
}
?>
<form action="thisForm.php" method="post">
<table>
<?
if ($error) { // if there were errors, display them now
echo $error;
}
?>
<tr><td>Please enter your email address<input type="text" name="email" value="<? echo $email ?>"></td></tr>
<tr><td><input type=submit name=submit value=submit></td></tr>
</table>
Hope this can give you some help