I have tried and tried and looked and looked, but I still can'y get this to work....
I am trying to (a)check form data on-the-fly, (b)once all required data is entered, then CLEAN the data and insert into MySQL database, and ONLY IF (a) and (b) have completed, then (c)send an email with the form data. See attached code
<html>
<body bgcolor = 'blue'>
<div align = 'center'>
<h1>test FORM</h1>
</div>
<p>
<?php
If ($_POST['submit']) //if the Submit button pressed
{
//collect form data
$fname = $_POST['FName'];
$lname = $_POST['LName'];
$email = $_POST['Email'];
$tel = $_POST['Tel'];
$mess = $_POST['Mess'];
//check required fields
$errorstring = "";
if (!$fname)
$errorstring = $errorstring."First Name<br>";
if (!$lname)
$errorstring = $errorstring."Last Name<br>";
if (!$email)
$errorstring = $errorstring."Email<br>";
if ($errorstring !="")
echo "<div align = 'center'><b>Please fill out the following fields:</b><br><font color = 'red'><b>$errorstring</b></font></div>";
else
{
//clean data and insert into database
$fname = mysql_real_escape_string($fname);
$lname = mysql_real_escape_string($lname);
$email = mysql_real_escape_string($email);
$tel = mysql_real_escape_string($tel);
$mess = mysql_real_escape_string($mess);
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mec", $con);
$sql="INSERT INTO formtest (FName, LName, Title, Tel, Email, Mess)
VALUES ('$fname','$lname','$title','$tel','$email','$mess')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
//if data is inserted successfully, send email
$to = 'my_email_address_here';
$subject = 'test form';
$message = 'Hello';
$headers = 'From: $email';
$sent = mail($to, $subject, $message, $headers);
if($sent)
{print "Email successfully sent";}
else
{print "There was an error sending the mail";}
mysql_close($con);
}
}
?>
<p>
<form action= 'testmecform.php' method= 'POST'>
<table width = '640' border = '0' align = 'center'>
<tr>
<td align = 'right'><b>First Name</b></td>
<td><input type = 'text' name = 'FName' value = '<?php echo $fname; ?>' size = '25'></td>
<td><div align = 'right'><b>Telephone</b></div></td>
<td><input type = 'text' name = 'Tel' value = '<?php echo $tel; ?>' size = '25'></td>
</tr>
<tr>
<td align = 'right'><b>Last Name</b></td>
<td><input type = 'text' name = 'LName' value = '<?php echo $lname; ?>' size = '25'></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td align = 'right'><b>Email</b></td>
<td><input type = 'text' name = 'Email' value = '<?php echo $email; ?>' size = '25'></td>
<td> </td>
<td> </td>
</tr>
<tr>
<th colspan = '4'><b>Please enter any additional information here:</b></th>
</tr>
<tr>
<th colspan = '4'><textarea name = 'Mess' cols = '50' rows = '10'><?php echo $mess; ?></textarea></th>
</tr>
<tr>
<th colspan = '4'><b>Please make sure all information is correct before submitting</b></th>
</tr>
<tr>
<th colspan = '4'><input type = 'submit' name = 'submit' value = 'Submit Form'></th>
</tr>
</table>
</form>
</body>
</html>
What is happening is:
(a) form makes sure that required fields are entered (although it doesn't check the format/validity just yet - I will include these additional validation checks.
(b) CLEANED form data is inserted into MySQL database
(c) I get the error message for the email portion (not sending the email).
Any Ideas????