php - HTML PHP 复选框
问题描述
我刚刚在我的联系表单中添加了一个复选框,以表明用户是否愿意注册我的时事通讯。我希望我的 PHP 脚本在向我发送电子邮件时输出复选框的值。我已经彻底搜索了互联网,找不到任何解决方案,只有遇到类似问题的人。 https://joebaileyphotography.com/contact%20me.html
HTML:
<form method="post" name="myemailform" action="form-to-email.php">
<label class="label" for="fname">First Name</label>
<br>
<input type="text" id="fname" name="fname" placeholder="Your name..">
<br>
<label class="label" for="lname">Last Name</label>
<br>
<input type="text" id="lname" name="lname" placeholder="Your last name..">
<br>
<label class="label" for="Email">Email Address</label>
<br>
<input type="text" id="Email" name="Email" placeholder="Your email address...">
<br>
<label class="label" for="subject">Subject</label>
<br>
<textarea id="subject" name="subject" placeholder="Drop me a line..." style="height:200px"></textarea>
<br>
<input type="checkbox" id="subscribeNews" name="subscribe" value="newsletter" checked>
<label for="subscribeNews">Yes, I would like to recieve news from Joe Bailey Photography.</label>
<br>
<br>
<input type="checkbox" id="privacyPolicy" name="privacy" value="privacy">
<label for="privacyPolicy">I consent to having this website store my submitted information so they can respond to my inquiry. For more info, read the <a href="/privacy-policy.html">Privacy Policy.</a></label>
<br>
<br>
<div class="g-recaptcha" data-sitekey="6LeqeRkTAAAAAFGmVFmAorEU9n0yL4NDEpSUnM0R"></div>
<br>
<input type="submit" value="Send Form">
PHP:
<?php
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => '6LeqeRkTAAAAABjyRvL0c1vrqG3ZmV51O0-S3xcz',
'response' => $_POST["g-recaptcha-response"]
);
$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
echo "<p>Please go back and verify you are human.</p>";
} else if ($captcha_success->success==true) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$visitor_email = $_POST['Email'];
$subject = $_POST['subject'];
$email_subject = "Joe Bailey Photography Contact Form";
$email_body = "You have received a new message from $fname $lname.\n".
"Here is the message:\n$subject
";
$to = "enquiries@joebaileyphotography.com";
$headers = "From: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: Contact%20Me%20Thank%20You.html#thankyou');
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
}
解决方案
要了解您从表单中收到的内容,请使用 var dump 它。
var_dump($_POST);
您会看到,当复选框被打勾时,您将privacy
在数组中具有钥匙。因此,您可以使用 if 条件,例如
if (array_key_exists('privacy', $_POST) && !empty($_POST['privacy'])) {
// send email
}
推荐阅读
- javascript - 无法从 electron-js 成功连接到 sqlite3 数据库
- kubernetes - 如何将与服务的连接限制为 Kubernetes 中的 pod 列表?
- javascript - 使用 Firebase 数据库进行分页/延迟加载
- asp.net-core - GraphQL Mutation 有关于变量的问题
- r - biplot R中特定点的文本标签
- python - 有没有办法使用 Mayavi 填充陀螺仪表面的一侧?
- reactjs - 如何不显示重复的元素?
- c# - System.Data.MissingPrimaryKeyException:“表没有主键。”
- android - Jetpack compose 中的可组合中心
- google-sheets - 如何使用 Google 财经检查股市是否开市(表格)