I have a form that asks for user input (strangely enough) and when the user clicks the 'Check' button, they are taken to another page called check.php, which checks validates required fields etc, and shows the user what they inputted... if all is correct, then then press submit, which calls another page, called submit.php (yeah im original... my imagination outdoes me sometimes)... this is the page that actually sends the data via email, and enters it into a database... now up until this page, everything has been ok... the data has parsed from my form.php to check.php, and displays data properly... once the submit button is pressed though, the data seems to dissapear and I get emailed a blank email!!!
Im obviously not parsing things correctly.. can anyone see what im doing wrong??
form.php
<form name="form" method="post" action="check.php">
Contact Name:<br />
<input type="text" name="contact_name" /><br /><br />
Company Name:<br />
<input type="text" name="company_name" /><br /><br />
Email Address:<br />
<input type="text" name="email_address" /><br /><br />
Contact Phone Number:<br />
<input type="text" name="contact_phone" /><br /><br />
<input type="submit" name="submit" value="Check Details" />
check.php
<?php
// Gather data from index.php
$name = $_POST['contact_name'];
$company = $_POST['company_name'];
$email = $_POST['email_address'];
// Check Name field
if(empty($name))
{
echo(" - <strong>Enter your Name please!</strong><br /><br />");
}
// Check email address for invalid character or blank spaces
function CheckMail($email) {
if (eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,4}$", $email)) { return true; }
else { return false; }
}
if ((empty($email)) || (!CheckMail($email)))
{
echo " - <strong>Your email is invalid!! Please click your back button to fix the errors.</strong><br /><br />";
}
// IF THEY ARE NOT EMPTY
if(!empty($name) && !empty($email) )
{
?>
<!-- back to html to show correct fields, and submit button -->
<p style="color: Green; font-size: x-large; font-weight:bold">Please check the details below to ensure they are correct before clicking submit</p>
<form name="confirm" method="post" action="submit.php">
<table border="0" cellpadding="10px">
<tr>
<td>Contact Name:</td>
<td><strong><? echo("$name"); ?></strong> <input type="hidden" name="name" value="<? echo("$name"); ?>" /></td>
</tr>
<tr>
<td>Company Name:</td>
<td><strong><? echo("$company"); ?></strong> <input type="hidden" name="name" value="<? echo("$company"); ?>" /></td>
</tr>
<tr>
<td>Email Address:</td>
<td><strong><? echo("$email"); ?></strong> <input type="hidden" name="name" value="<? echo("$email"); ?>" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit_form" value="Submit Details" /></td>
</tr>
</table>
</form>
<!-- back to php to close the script -->
<? }
}
}
?>
and finally submit.php
<?php
if (!isset($_POST['submit_form'])) {
header( 'Location: http://www.domain/test/form.php' );
}
else {
// Declare address that mail is to send to
$sendTo = "chris@domain.co.nz";
// Get variables from check.php
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
// Create HTML email to send to $sentTo
$todaysdate = gmdate("l j F Y" );
$subject = "New Website Enquiry From: $name";
$message = "
<h1>New Enquiry</h1>
<p> </p>
<p>Received: $todaysdate</p>
<p> </p>
<p> </p>
<h3 style='color: red'>Contact Details</h3>
<p><span style='font-weight: bold'>Contact Name:</span> $name</p>
<p><span style='font-weight: bold'>Company Name:</span> $company</p>
<p><span style='font-weight: bold'>Email Address:</span> $email</p>
";
$header = "FROM: Online Enquiry <$email>\r\nContent-type: text/html; charset=ISO-8859-1\r\n";
mail($sendTo, $subject, $message, $header);
// Database connection and select/insert details //
// Connection and Database Details //
$servername = 'localhost';
$dbusername = 'user';
$dbpassword = 'pwd';
$dbname = 'db';
$con = mysql_connect($servername, $dbusername, $dbpassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("enquiries", $con);
// INSERT into database //
$sql = "INSERT INTO bookings (name, company, email) VALUES ('$name', '$company', '$email')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con);
echo "Recorded $name in Database";
?>
<!-- What is shown by the browser if the send has been successful -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Enquiry</title>
</head>
<body>
<p>Dear <?php echo("$name"); ?>,</p>
<p>Thank you for your submission.</p>
</body>
</html>
<?
}
?>