0

我有一个简单的联系表格,其中包含一个蜜罐输入字段。如果填写了该字段,我希望表单重定向到网页。

我尝试了下面的代码,但它给了我一个错误:AJAX 请求失败!

所以我知道我做错了什么。我敢肯定这很简单。

谢谢

php代码:

    if(!empty($_POST["e-mail"])) header('Location: blankman.html');exit;

表单输入:

    <input type="text" name="e-mail" id="e-mail"/>

这是完整的php代码:

    <?php

if(!empty($_POST["e-mail"])) header('Location: blankman.html');exit;

// Clean up the input values
foreach($_POST as $key => $value) {
  if(ini_get('magic_quotes_gpc'))
    $_POST[$key] = stripslashes($_POST[$key]);

  $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];


// Test input values for errors
$errors = array();
if(strlen($name) < 2) {
  if(!$name) {
    $errors[] = "missing your name";
  } else {
    $errors[] = "your name must be 2 characters";
  }
}

if(!$email) {
  $errors[] = "missing your email";
} else if(!validEmail($email)) {
  $errors[] = "you must enter a valid email";
}
if(strlen($message) < 3) {
  if(!$message) {
    $errors[] = "missing your message";
  } else {
    $errors[] = "oops! your message is not long enough";
  }
}

if($errors) {
  // Output errors and die with a failure message
  $errortext = "";
  foreach($errors as $error) {
    $errortext .= "<li>".$error."</li>";
  }
$response = array(
    "success" => false,
    "content" => "<span class='failure'><ul>". $errortext ."</ul></span>"
);
die(json_encode($response));
}

// Send the email *********** enter your email address and message info ***
$to = "myemail@myemail.com"; 
$subject = "Website message from: $name";
$message = "From:\n$name\n\nEmail:\n$email\n\nMessage:\n$message";
$headers = "From: $email";

mail($to, $subject, $message, $headers);

// Die with a success message
$response = array(
    "success" => true,
    "content" => "<span class='success'><li>Thank you! Your message has been sent :).</li></span>"
);
die(json_encode($response));

// A function that checks to see if
// an email is valid
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

?>

这是javascript:

<script>
    $(document).ready(function () {
        $("#contactform").submit(function (e) {
            e.preventDefault();
            var t = $(this).attr("action");
            var n = $(this).serialize();
            $.post(t, n, null, "json").done(function (e) {
                if (e.success) {
                    $("#success").html(e.content);
                    $("#contactform,#error").hide()
                } else {
                    $("#error").html(e.content)
                }
            }).fail(function () {
                alert("The AJAX request failed!")
            })
        })
    })
</script>
4

1 回答 1

2

如果蜜罐已满,则不要显示任何不同的行为。这样你就可以用大红字向垃圾邮件发送者大喊:“这是一个蜜罐!调查并写一个解决方法!”

始终以与常规请求完全相同的方式响应垃圾邮件请求

if(!empty($_POST["e-mail"])) {
    $response = array(
        "success" => true,
        "content" => "<span class='success'><li>Thank you! Your message has been sent :).</li></span>"
    );
    die(json_encode($response));
}
于 2013-08-08T16:45:10.137 回答