Reputation: 121
I have a simple php page that contains a form. When submitted it should send 2 emails(one to me and one to my boss), and insert its data into my database. Instead its sending 4 emails (two to me and two to my boss) and inserting 2 rows of identical data instead of one. Any idea why this might be happening?
EDIT: I've found that the problem only occurs when you refresh the page, and each time you refresh the page it sends out a new copy of the email/data.
Here's my PHP (if you see removed I removed what was in the quotation marks for security reasons)
if (isset($_POST['submitted'])){
define('DB_NAME', 'removed');
define('DB_USER', 'removed');
define('DB_PW', 'removed');
define('DB_HOST', 'removed');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PW, DB_NAME);
if(!$link){
die('Test Connection Failed 1: ' . mysqli_error());
}
//Name
$FName = $_POST['FName'];
$FName = mysqli_real_escape_string($link, $FName);
$LName = $_POST['LName'];
$LName = mysqli_real_escape_string($link, $LName);
$Company = $_POST['Company'];
$Company = mysqli_real_escape_string($link, $Company);
//Phone
$Phone1 = $_POST['Phone1'];
$Fax = $_POST['Fax'];
//Address
$Street = $_POST['Street'];
$Street = mysqli_real_escape_string($link, $Street);
$City = $_POST['City'];
$City = mysqli_real_escape_string($link, $City);
$State = $_POST['State'];
$State = mysqli_real_escape_string($link, $State);
$Zip = $_POST['Zip'];
$Zip = mysqli_real_escape_string($link, $Zip);
$Country = $_POST['Country'];
$Country = mysqli_real_escape_string($link, $Country);
//Message
$Message = $_POST['Message'];
$Message = mysqli_real_escape_string($link, $Message);
//Email
$Email = $_POST['Email'];
$Email = mysqli_real_escape_string($link, $Email);
//Email
$Email2 = $_POST['Email2'];
$Email2 = mysqli_real_escape_string($link, $Email);
$sqlInsert = "INSERT INTO `removed`.`removed`(`ID`, `FName`, `LName`, `Company`, `Phone`, `Fax`, `Street`, `City`, `State`, `Country`, `Zipcode`, `Email`, `Message`) VALUES (NULL, '".$FName."', '".$LName."', '".$Company."', '".$Phone1."', '".$Fax."', '".$Street."', '".$City."', '".$State."', '".$Country."', '".$Zipcode."', '".$Email."', '".$Message."')";
if(!mysqli_real_query($link, $sqlInsert)){
echo "Failed To Post To Database";
} else {
mysqli_close($link);
}
//Validate first
if(empty($LName)||empty($Email))
{
echo "Name and email are mandatory!";
exit;
}
$email_from = $Email;
$email_subject = "Bel Air Contact Form";
$email_body = '<html><body>';
$email_body .= '<table border="1" cellpadding="15">';
$email_body .= "<tr><td bgcolor='#66CCFF' width='150px'><strong>Name:</strong> </td><td bgcolor='#66CCFF' width='400px'>" . $FName . " " . $LName . "</td></tr>";
$email_body .= "<tr><td bgcolor='#66FFFF' width='150px'><strong>Company:</strong> </td><td bgcolor='#66FFFF' width='400px'>" . $Company . "</td></tr>";
$email_body .= "<tr><td bgcolor='#66CCFF' width='150px'><strong>Email Address:</strong> </td><td bgcolor='#66CCFF' width='400px'>" . $Email . "</td></tr>";
$email_body .= "<tr><td bgcolor='#66FFFF' width='150px'><strong>Secondary Email Address:</strong> </td><td bgcolor='#66FFFF' width='400px'>" . $Email2 . "</td></tr>";
$email_body .= "<tr><td bgcolor='#66CCFF' width='150px'><strong>Street:</strong> </td><td bgcolor='#66CCFF' width='400px'>" . $Street . "</td></tr>";
$email_body .= "<tr><td bgcolor='#66FFFF' width='150px'><strong>City:</strong> </td><td bgcolor='#66FFFF' width='400px'>" . $City . "</td></tr>";
$email_body .= "<tr><td bgcolor='#66CCFF' width='150px'><strong>State:</strong> </td><td bgcolor='#66CCFF' width='400px'>" . $State . "</td></tr>";
$email_body .= "<tr><td bgcolor='#66FFFF' width='150px'><strong>Country:</strong> </td><td bgcolor='#66FFFF' width='400px'>" . $Country . "</td></tr>";
$email_body .= "<tr><td bgcolor='#66CCFF' width='150px'><strong>Zipcode:</strong> </td><td bgcolor='#66CCFF' width='400px'>" . $Zipcode . "</td></tr>";
$email_body .= "<tr><td bgcolor='#66FFFF' width='150px'><strong>Phone:</strong> </td><td bgcolor='#66FFFF' width='400px'>" . $Phone1 . "</td></tr>";
$email_body .= "<tr><td bgcolor='#66CCFF' width='150px'><strong>Fax:</strong> </td><td bgcolor='#66CCFF' width='400px'>" . $Fax . "</td></tr>";
$email_body .= "<tr><td bgcolor='#66FFFF' width='150px'><strong>Message:</strong> </td><td bgcolor='#66FFFF' width='400px'>" . $Message . "</td></tr>";
$email_body .= "</table>";
$email_body .= "</body></html>";
$rbailey = "removed";
$info = "removed";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $Email \r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($rbailey,$email_subject,$email_body,$headers);
mail($info,$email_subject,$email_body,$headers);
} //End of ISSET if statement
?>
If needed I can include the form but its a very basic form with a ton of fields so I left it out so my post isn't insanely long.
Upvotes: 1
Views: 52
Reputation: 74217
For the record, you have a missing dot in one of your headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
^
That alone will break your headers.
The issue appears to be due to a page refresh.
To remedy this, add a header after your last mail()
call, and make sure you're not outputting before header.
I.e.:
mail($info,$email_subject,$email_body,$headers);
header("Location: http://www.example.com");
exit;
In your form, add autocomplete="off"
in your form, and/or your input fields.
<form action="your_page.php" method="post" autocomplete="off">
Which is useful in HTML5 to clear any previously entered contents in a form.
Here is a link you can consult:
Sidenote:
Just to save you quite a few keystrokes; you can just do:
$FName = mysqli_real_escape_string($link, $_POST['FName']);
etc. rather than defining everything twice.
Upvotes: 1