I am using this code on my website (It is included in 404.php):
<?php
if (isset($_POST["op"]) && ($_POST["op"]=="send")) {
/******** START OF CONFIG SECTION *******/
$sendto = "myemail@gmail.com";
$subject = "Website Contact Enquiry";
/******** END OF CONFIG SECTION *******/
$message = $HTTP_POST_VARS['message'];
$headers = "From: $email\n";
$headers . "MIME-Version: 1.0\n"
. "Content-Transfer-Encoding: 7bit\n"
. "Content-type: text/html; charset = \"iso-8859-1\";\n\n";
$request = $_SERVER["REQUEST_URI"];
$self = $_SERVER["PHP_SELF"];
// Build the email body text
$emailcontent = "
-----------------------------------------------------------------------------
WEBSITE CONTACT ENQUIRY
-----------------------------------------------------------------------------
$request
$self
$message
";
if (!trim($message)) {
echo "<p>Please go back and type a Message</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}
// Sends out the email or will output the error message
elseif (mail($sendto, $subject, $emailcontent, $headers)) {
echo "<br><br><p><b>Thank You </b></p><p>We will be in touch as soon as possible.</p>";
}
}
else {
?>
And this is my form
<form action="http://www.ahornblume.ch/404.php" method="post">
<INPUT NAME="op" TYPE="hidden" VALUE="send">
<textarea name="message" cols="25" rows="4" placeholder="Wenn Sie eine Antwort wünschen, vergessen Sie bitte ihre email Adresse nicht."></textarea><br>
<input name="submit" type="submit" value="Send Message">
</form>
I am redirecting all 404 errors in my htaccess file like so:
ErrorDocument 404 /404.php
If I go to ahornblume.ch/404.php everything works and I can use the form to send emails.
But, if I go to ahornblume.ch/something the contact form doesn’t work.
Your form does not have an action specified so it will submit to the URL that was used to display the form.
In case of
ahornblume.ch/404.phpit will submit to 404.php and it will work.In case of
ahornblume.ch/somethingit will result in a 404 error that will just display the 404.php page, but the form post will have been discarded.To fix, specify an action with an absolute path
/404.php