php - POST方法不适用于联系论坛
问题描述
我试图让 id="RecipientInput" 来处理我的 PHP 代码,但它似乎不起作用。在我这样做之后,我还想添加 reCAPTCHA,但我认为我是一个失败的原因。如果我无法弄清楚这一点,我就无法弄清楚如何从谷歌实现反垃圾邮件 API。
HTML
<form name="contactForm" id="contactForm" method="post" action="" novalidate="novalidate">
<fieldset>
<div class="form-field">
<input name="contactName" type="text" id="contactName" placeholder="Your Name" value="" minlength="2" required="" aria-required="true" class="full-width">
</div>
<div class="form-field">
<input name="contactEmail" type="email" id="contactEmail" placeholder="Your Email" value="" required="" aria-required="true" class="full-width">
</div>
<div class="form-field">
<input name="contactSubject" type="text" id="contactSubject" placeholder="Subject" value="" class="full-width">
</div>
<div class="form-field cl-custom-select">
<select class="full-width" id="RecipientInput">
<option value="Option 1">General Enquiry</option>
<option value="Option 2">Pivot</option>
<option value="Option 3">Napkin</option>
</select>
</div>
<div class="form-field">
<textarea name="contactMessage" id="contactMessage"placeholder="Your Message" rows="10" cols="50" required="" aria-required="true" class="full-width"></textarea>
</div>
<div class="form-field">
<button class="full-width btn-primary">Submit</button>
<div class="submit-loader">
<div class="text-loader">Sending...</div>
<div class="s-loader">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
</div>
</fieldset>
</form>
PHP
<?php
// Replace this with your own email address
$siteOwnersEmail = 'my email address';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
// $selected_option = trim(stripslashes($_POST['RecipientInput']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
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 Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "<strong>Email from: </strong>" . $name . "<br />";
$message .= "<strong>Email address: </strong>" . $email . "<br />";
// $message .= "<strong>Selected Option: </strong>" . $selected_option . "<br />";
$message .= "<strong>Message: </strong><br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from www.example.com </a>contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
我正在尝试在我的测试服务器上连续测试,但它目前给我带来了问题。
解决方案
推荐阅读
- php - 为什么我在 function.php 上的动作挂钩不执行?
- gams-math - 为什么我的对称差异不正确?
- python - AttributeError: 'Image' object has no attribute 'new' 在尝试使用 Pytorchs AlexNet Lighting 预处理时发生
- python - 如何复制类的实例?
- c# - RestSharp,Forge API - 出现错误:文件上传时范围重叠
- c# - 如何在 SOAP 标头中声明命名空间前缀
- php - 如果找不到路由,如何处理 404 作为 json 响应?
- wordpress - 将 wordpress 网站从本地移动到现场时出现 404 错误?
- java - Android 下载 AsyncTask 冻结了我的应用很长时间
- python - 从单独的文件python导入函数