In reply to:
we are wanting the answers that are submitted from the form mail to go into a database and be stored somewhere.
Given a form like this one:
<?php
session_start();
$title = ($_SESSION['title']) ? $_SESSION['title'] : "";
$first_name = ($_SESSION['first_name']) ? $_SESSION['first_name'] : "";
$last_name = ($_SESSION['last_name']) ? $_SESSION['last_name'] : "";
$email = ($_SESSION['email']) ? $_SESSION['email'] : "";
$day_phone = ($_SESSION['day_phone']) ? $_SESSION['day_phone'] : "";
$night_phone = ($_SESSION['night_phone']) ? $_SESSION['night_phone'] : "";
$cell_phone = ($_SESSION['cell_phone']) ? $_SESSION['cell_phone'] : "";
$street1 = ($_SESSION['street1']) ? $_SESSION['street1'] : "";
$street2 = ($_SESSION['street2']) ? $_SESSION['street2'] : "";
$city = ($_SESSION['city']) ? $_SESSION['city'] : "";
$state = ($_SESSION['state']) ? $_SESSION['state'] : "";
$zip = ($_SESSION['zip']) ? $_SESSION['zip'] : "";
$comments = ($_SESSION['comments']) ? $_SESSION['comments'] : "";
$error = ($_SESSION['error']) ? $_SESSION['error'] : "";
header("Content-Type: text/html;charset=UTF-8");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Form</title>
</head>
<body>
<h1>Contact Form</h1>
<form method="post" action="submit.php">
<?php
if($error != "") {
print " <p class="error">$error</p>n";
}
?>
<p>Please note: Required information is marked with an asterisk (*).</p>
<dl>
<dt>Title*:</dt>
<dd><input type="text" size="40" name="title" value="<?php print $title ?>" /></dd>
<dt>First Name*:</dt>
<dd><input type="text" size="40" name="first_name" value="<?php print $first_name ?>" /></dd>
<dt>Last Name*:</dt>
<dd><input type="text" size="40" name="last_name" value="<?php print $last_name ?>" /></dd>
<dt>Email Address*:</dt>
<dd><input type="text" size="40" name="email" value="<?php print $email ?>" /></dd>
<dt>Daytime Phone Number*:</dt>
<dd><input type="text" size="40" name="day_phone" value="<?php print $day_phone ?>" /></dd>
<dt>Evening Phone Number*:</dt>
<dd><input type="text" size="40" name="night_phone" value="<?php print $night_phone ?>" /></dd>
<dt>Cell Phone Number:</dt>
<dd><input type="text" size="40" name="cell_phone" value="<?php print $cell_phone ?>" /></dd>
<dt>Street Address 1*:</dt>
<dd><input type="text" size="40" name="street1" value="<?php print $street1 ?>" /></dd>
<dt>Street Address 2:</dt>
<dd><input type="text" size="40" name="street2" value="<?php print $street2 ?>" /></dd>
<dt>City*:</dt>
<dd><input type="text" size="40" name="city" value="<?php print $city ?>" /></dd>
<dt>State*:</dt>
<dd><input type="text" size="40" name="state" value="<?php print $state ?>" /></dd>
<dt>Zip*:</dt>
<dd><input type="text" size="40" name="zip" value="<?php print $zip ?>" /></dd>
<dt>Comments:</dt>
<dd><textarea name="comments" rows="10" cols="30"><?php print $comments ?></textarea></dd>
</dl>
<p>
<input type="hidden" name="error" value="" />
<input type="submit" name="submit" value="Send Your Information" />
</p>
</form>
</body>
</html>
You could feed the data into a database with this:
<?php
session_start();
session_register("title");
session_register("first_name");
session_register("last_name");
session_register("email");
session_register("day_phone");
session_register("night_phone");
session_register("cell_phone");
session_register("street1");
session_register("street2");
session_register("city");
session_register("state");
session_register("zip");
session_register("comments");
session_register("error");
// check if form was submitted
if(!$_POST) {
header("Location: /");
exit;
}
header("Content-Type: text/html;charset=UTF-8");
// convert POST variables into local variables
$title = $_POST['title'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$day_phone = $_POST['day_phone'];
$night_phone = $_POST['night_phone'];
$cell_phone = $_POST['cell_phone'];
$street1 = $_POST['street1'];
$street2 = $_POST['street2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$comments = $_POST['comments'];
$error = $_POST['error'];
// create session variables
$_SESSION['title'] = $title;
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['email'] = $email;
$_SESSION['day_phone'] = $day_phone;
$_SESSION['night_phone'] = $night_phone;
$_SESSION['cell_phone'] = $cell_phone;
$_SESSION['street1'] = $street1;
$_SESSION['street2'] = $street2;
$_SESSION['city'] = $city;
$_SESSION['state'] = $state;
$_SESSION['zip'] = $zip;
$_SESSION['comments'] = $comments;
$_SESSION['error'] = $error;
$regexp = "^([_a-z0-9-] )(.[_a-z0-9-] )*@([a-z0-9-] )(.[a-z0-9-] )*(.[a-z]{2,4})$";
// check required fields
if($title == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a title.";
header("Location: /contact/");
exit;
}
if($first_name == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a first name.";
header("Location: /contact/");
exit;
}
if($last_name == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a last name.";
header("Location: /contact/");
exit;
}
if($email == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered an email address.";
header("Location: /contact/");
exit;
}
// check if email address is valid
if(!eregi($regexp,$email)) {
$_SESSION['error'] = "The email address you have entered appears to be invalid. Please enter a valid email address.";
header("Location: /contact/");
exit;
}
if($day_phone == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a daytime phone number.";
header("Location: /contact/");
exit;
}
if($night_phone == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered an evening phone number.";
header("Location: /contact/");
exit;
}
if($street1 == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a street address.";
header("Location: /contact/");
exit;
}
if($city == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a city.";
header("Location: /contact/");
exit;
}
if($state == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a state.";
header("Location: /contact/");
exit;
}
if($zip == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a zip code.";
header("Location: /contact/");
exit;
}
// If the script gets down to here, the user has entered everything correctly!
// get IP address
$ip = $_SERVER['REMOTE_ADDR'];
// make timestamp
$now = time();
$timestamp = date("YmdHis",$now);
// initialize database variables
$hostname = ""; // your hostname
$username = ""; // your database username
$password = ""; // your database password
$database = ""; // the name of your database
// connect to database
$link = mysql_connect($hostname,$username,$password);
mysql_select_db($database);
// insert information into database
$sql = "insert into contacts (timestamp, ip, title, first_name, last_name, email, day_phone, night_phone, cell_phone,
street1, street2, city, state, zip, comments) values ('$timestamp', '$ip', '$title', '$first_name', '$last_name',
'$email', '$day_phone', '$night_phone', '$cell_phone', '$street1', '$street2', '$city', '$state', '$zip', '$comments')";
$query = mysql_query($sql,$link) or die("Could not insert data: ".mysql_error());
// compose email
$message = "Thank you for submitting comments. Here is the information you sent to us:nn";
$message = $message."Title: $titlen";
$message = $message."First Name: ".stripslashes($first_name)."n";
$message = $message."Last Name: ".stripslashes($last_name)."n";
$message = $message."Email: $emailn";
$message = $message."Daytime Phone Number: $day_phonen";
$message = $message."Evening Phone Number: $night_phonen";
$message = $message."Cellular Phone: $cell_phonen";
$message = $message."Street Address 1: ".stripslashes($street1)."n";
$message = $message."Street Address 2: ".stripslashes($street2)."n";
$message = $message."City: ".stripslashes($city)."n";
$message = $message."State: $staten";
$message = $message."Zip Code: $zipn";
$message = $message."Comments:n".stripslashes($comments);
// send mail
$to = "$email";
$from = ""; // your email address
$subject = "Contact Repsonse";
mail ( "$to", "$subject", "$message", "$from" );
// redirect to home page once finished
header("Location: /");
?>
That script will add the information into a database, and then send a confirmation email back to the sender. It'll also send the user back to the form if required fields are missing (which is why I've made use of session variables).
You could access the information from a simple table:
<?php
// initialize database variables
$hostname = ""; // your hostname
$username = ""; // your database username
$password = ""; // your database password
$database = ""; // the name of your database
// connect to database
$link = mysql_connect($hostname,$username,$password);
mysql_select_db($database);
// get contacts data
$sql = "select * from contacts order by timestamp desc";
$query = mysql_query($sql,$link) or die("Could not retrieve contacts: ".mysql_error());
function make_date($timestamp) {
$yr = substr($timestamp,0,4);
$mo = substr($timestamp,4,2);
$dy = substr($timestamp,6,2);
$hr = substr($timestamp,8,2);
$mn = substr($timestamp,10,2);
$sc = substr($timestamp,12,2);
$timestamp = date("l, F d, Y",mktime($hr,$mn,$sc,$mo,$dy,$yr));
return $timestamp;
}
header("Content-Type: text/html;charset=UTF-8");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>List of contacts</title>
</head>
<body>
<h1>List of Contacts</h1>
<table cellspacing="0px">
<thead>
<tr>
<th>Title</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Daytime Phone</th>
<th>Evening Phone</th>
<th>Cell Phone</th>
<th>Street Address 1</th>
<th>Street Address 2</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Comments</th>
<th>Date submitted</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysql_fetch_assoc($query)) {
$title = $row['title'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$email = $row['email'];
$day_phone = $row['day_phone'];
$night_phone = $row['night_phone'];
$cell_phone = $row['cell_phone'];
$street1 = $row['street1'];
$street2 = $row['street2'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$comments = $row['comments'];
$timestamp = $row['timestamp'];
print " <tr>n";
print " <td>$title</td>n";
print " <td>$first_name</td>n";
print " <td>$last_name</td>n";
print " <td><a href="mailto:$email">$email</a></td>n";
print " <td>$day_phone</td>n";
print " <td>$night_phone</td>n";
print " <td>$cell_phone</td>n";
print " <td>$street1</td>n";
print " <td>$street2</td>n";
print " <td>$city</td>n";
print " <td>$state</td>n";
print " <td>$zip</td>n";
print " <td>".nl2br($comments)."</td>n";
print " <td>".make_date($timestamp)."</td>n";
print " </tr>n";
}
?>
</tbody>
</table>
</body>
</html>
I hope you find that useful.