<?php
// basic signup form script - submits to your email address...
$ouremail = '[email protected]'; // enter the email address to submit to
// a useful function to get submission data...
function getstr($name){ return isset($_POST[$name]) ? trim(get_magic_quotes_gpc()==1 ? stripslashes($_POST[$name]) : $_POST[$name]) : '';}
// another useful function to validate email addresses
function validateemail($email){
if (eregi("(@.*@)(\.\.)|(@\.)|(\.@)|(^\.)", $email) ||
!eregi ("^.+\@(\[?)[-_a-zA-Z0-9\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", $email)) {
return(false);
}else{
list($user, $domain) = explode("@", $email);
if ((!eregi("^[_a-zA-Z0-9\.\-]+$", $user)) ||
(!eregi("^[_a-zA-Z0-9\.\-]+$", $domain))) {
return false;
} else {
return(true);
}
}
}
// initialize variables used in the submission and the form
$name = getstr('name');
$email = getstr('email');
$street = getstr('street');
$city = getstr('city');
$state = getstr('state');
$zip = getstr('zip');
$submitted = false; // used to determine if the form has been submitted
$errors = ''; // holds any error messages we generate
// check if the form has been submitted...
if( isset($_POST['submit']) ){
// check that the submitted values are valid and store error messages
// to display (if any).
// validation is very basic - we just make sure the user has entered something
if( $email == '' ){
$errors = 'You must enter your email address.<br />';
}elseif( !validateemail($email) ){
$errors = 'You must enter a valid email address.<br />';
}
if( strlen($name) < 2 ) $errors .= 'You must enter your name.<br />';
if( strlen($state) < 2 ) $errors .= 'You must enter your state.<br />';
if( strlen($city) < 2 ) $errors .= 'You must enter your city.<br />';
if( strlen($zip) < 2 ) $errors .= 'You must enter you zip.<br />';
if( $errors == '' ){ // if no errors, email the submission to us
// just some extra info to include in the email...
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']; // url of this script
$ip = isset($_SERVER['X_FORWARDED_FOR']) ? $_SERVER['X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; // ip address of user
$date = date('l jS F Y @ H:i'); // the current date and time in an easy to read format
// set up the email message...
$msg = 'Submission from script at '.$url.' from ip address "'.$ip.'" on '.$date;
$msg .= "\n\nThe following values were submitted:\n\n";
$msg .= "Name: $name\nEmail: $email\nAddress:\n$street\n$city\n$state\n$zip";
// create headers to send with the email...
$headers = "From: $email\r\nReply-To: $email\r\nErrors-To: $ouremail\r\nReturn-Path: $ouremail";
// send the email to ourselves...
@mail($ouremail, 'signup form submission', $msg, $headers);
$submitted = true;
}
}
?>
<html>
<head>
<title>Basic form to email example</title>
</head>
<body>
<h3>Basic Form To Email Example</h3>
<?php
if( $submitted ){ // if the form has been submitted, display a message
?>
<h4>Thank-you for signing up.</h4>
Your details have been sent to our email address. Thanks. Muchly.
<?php
}else{ // if the form hasn't been submitted, display the form
?>
<h4>Sign Up</h4>
<?php
// if the user submitted the form, but didnt fill everything in, display an error message
// above the form telling them to fill it all in. OR ELSE. :(
if( $errors != '' ){
?>
<div style="font-weight: bold; color: red;">
Please correct the following errors:<br /><br />
<?php echo $errors;?>
</div>
<?php
}
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
<b>Your Name</b>
<br />
<input type="text" name="name" value="<?php echo htmlspecialchars($name);?>" />
<br />
<b>Your Email</b>
<br />
<input type="text" name="email" value="<?php echo htmlspecialchars($email);?>" />
<br />
<b>Street Address</b>
<br />
<input type="text" name="street" value="<?php echo htmlspecialchars($street);?>" />
<br />
<b>City</b>
<br />
<input type="text" name="city" value="<?php echo htmlspecialchars($city);?>" />
<br />
<b>State</b>
<br />
<input type="text" name="state" value="<?php echo htmlspecialchars($state);?>" />
<br />
<b>Zip</b>
<br />
<input type="text" name="zip" value="<?php echo htmlspecialchars($zip);?>" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
}
?>
</body>
</html>