Gensender formular
HejJeg har et problem med min kontaktformular. Når jeg har sendt den og trykker opdater i browseren bliver den samme email sendt igen og man kan på den måde spamme min email.
Kan man gøre noget ved dette udover tilføje AJAX?
contact.php:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contact_form" method="post">
<?php echo $response; ?>
<div id="form_fields">
<input type="hidden" name="string" id="string" value="<?php echo $new_string; ?>" />
<p><label for="name">Name:</label>
<input class="text_field required" minlength="2" type="text" name="name" id="name" /></p>
<p><label for="email">Email:</label>
<input class="text_field required" type="text" name="email" id="email" /></p>
<p><label for="subject">Subject:</label>
<input class="text_field required" type="text" name="subject" id="subject" /></p>
<p><label for="message">Message:</label>
<textarea class="text_area required" rows="10" name="message" id="message" ></textarea></p>
<p><label for="verification">Verification</label>
<?php
echo "<img class=\"verification_image\" src=\"verification.png\" alt=\"Verification\" />";
echo "<input class=\"text_field verification_input required\" name=\"verification\" id=\"verification\" type=\"text\" value=\"\" /></p>";
?>
<span class="button_margin"><input type="submit" value="Submit" name="submit" id="submit" class="button" /></span>
</div> <!-- end form_fields -->
</form>
og sendEmail.php:
<?php
if(isset($_POST['submit'])) {
$string = $_POST['string'];
$name = trim($_POST['name']);
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$verification = $_POST['verification'];
$site_owners_email = 'steffan@lildholdt.dk'; // Replace this with your own email address
$site_owners_name = 'Test Name'; // Replace with your name
// Check for error in the name field
if (empty($name)) {
$error['name'] = "Please enter your full name";
}
// Check for error in the email field
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address";
}
// Check for error in the subject field
if (empty($subject)) {
$error['subject'] = "Please enter a subject";
}
// Check for error in the message field
if (empty($message)) {
$error['message'] = "Please enter a message.";
}
// Check for error in the verification field
if ($verification != $string) {
$error['verification'] = "The verification code was wrong.";
}
// If no error occured send the email
if (!$error) {
require_once('phpMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From = $email;
$mail->FromName = $name;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->AddAddress($site_owners_email, $site_owners_name);
$mail->Body = $message;
$mail->Send();
$response = "<ul class=\"email_succes\"> <li> Congratulations, " . $name . ". We've received your email. We'll be in touch as soon as we possibly can. </li></ul>";
} // end if no error
else {
$response = "<ul class=\"email_error\">";
$response .= (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null;
$response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null;
$response .= (isset($error['subject'])) ? "<li>" . $error['subject'] . "</li> \n" : null;
$response .= (isset($error['message'])) ? "<li>" . $error['message'] . "</li>" : null;
$response .= (isset($error['verification'])) ? "<li>" . $error['verification'] . "</li>" : null;
$response .= "</ul>";
} // end if there was an error sending
}
?>