0

我在下面拼凑了一些 PHP 代码,这些代码通过 Ajax 表单提交创建了一个漂亮的 HTML 电子邮件,并带有一个表格。但是,我想让它在输入的时间范围小于 48 小时时弹出错误消息。我想知道改变 html 和 php 以使其工作的最简单方法是什么,或者是否有可能。

HTML 代码

<input name="Event Date" type="text" required id="Event Date" style="border-radius:5px; height:25px; font-family:'Quicksand', sans-serif; color:#666" title="Event Date" value="MM/DD/YYYY">

PHP 代码

    <?php

 require_once('recaptchalib.php');
 $privatekey = "###########################";
 $resp = recaptcha_check_answer ($privatekey,
                                 $_SERVER["REMOTE_ADDR"],
                                 $_POST["recaptcha_challenge_field"],
                                 $_POST["recaptcha_response_field"]);
unset($_POST["recaptcha_challenge_field"]);
unset($_POST["recaptcha_response_field"]);
 if (!$resp->is_valid) {
   // What happens when the CAPTCHA was entered incorrectly
   die ("The human verification code above WAS NOT entered correctly, please reset the human verification code and try again :-) " .
        "(Error: " . $resp->error . ")");
 } else {
     // Code Below For Handling Form (Send Email)
   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;
}

//Reject hyperlinks
$SpamErrorMessage = "No Websites URLs permitted";
 if (preg_match("/http/i", "$name")) {echo "$SpamErrorMessage"; exit();}
 if (preg_match("/http/i", "$email")) {echo "$SpamErrorMessage"; exit();}
 if (preg_match("/http/i", "$Special Message")) {echo "$SpamErrorMessage"; exit();}

 //48 Hour Time Limit
if ((strptime($_POST['Event Date'], "%m/%d/%Y") - time()) < (48 * 60 * 60)) {echo "Date is under 48 hours notice. Please call for service."; exit();}

//Where is it being sent?
$destination = "handle@domain.com";
$email_from = $_POST['Email'];

if (!validEmail($email_from))
    die("Invalid email address");

$message = "<html>
<body style=\"font-family:Arial; font-size:10pt;\">
Hi,<br>
You have recieved an online order:<br><br>
<table width='600' border='1' cellspacing='3'>";

//Gather posted variables:
foreach($_POST as $keys => $vars){
    if (empty($vars)) continue; #skip if vars is empty
    $message .= "<tr>
    <td bgcolor='#CCCCCC'><b>$keys:</b></td> <td><b><font color='red'>$vars</font></b></td>
    </tr>";
}
$message = str_replace("_"," ", $message);
$message .= "
</table>
</body>
</html>
";

#separating headers properly
$headers = "From: $email_from\r\n".
'Reply-To: '.$email_from."\r\n".
"Content-Type: text/html; charset=\"utf-8\"\r\n".
"Content-Transfer-Encoding: 7bit\r\n".
"MIME-Version: 1.0\n";

mail($destination,"Online Catering Form Results",$message,$headers);

echo "Thank you for your order!";
 }
 ?>
4

1 回答 1

0

尝试这样的事情,看看 Event Time 和 Time 实际被评估为什么。

//48 Hour Time Limit
if ((strptime($_POST['Event Date'], "%m/%d/%Y") - time()) < (48 * 60 * 60)) {
    echo "Event Time: " . strptime($_POST['Event Date']) . "\n";
    echo "Time: " . time() . "\n";
    echo "Date is under 48 hours notice. Please call for service."; 
    exit();
}
于 2013-09-07T16:31:46.973 回答