[英]validate php contact form with checkbox
我正在尝试向联系表单添加另一个验证。 我需要在表单底部选中一个复选框,供用户选中后才能提交表单。
这是我到目前为止验证表格的内容。 我想知道如何添加复选框,以便仅在选中复选框的情况下才提交表单
<section class="form wow fadeInRight" data-wow-duration="2s"> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3 contact-form"> <h2>Contact Us</h2> <p>Please do not hesitate to get in touch with us by filling out the form or using the contact details below</p> <form class="form-horizontal" action="" id="form" method="post" name="form"> <input type="hidden" name="bts" value="" /> <input type="hidden" name="page" value="<?php echo $current_url; ?>" /> <div class="form-group"> <div class="col-md-12"> <input type="text" class="form-control" id="name" name="name" placeholder="Your Name:*" value="<?php echo $send_data['name']; ?>" required> </div> </div> <div class="form-group"> <div class="col-md-12"> <input type="email" class="form-control" id="email" name="email" placeholder="Email Address:*" value="<?php echo $send_data['email']; ?>" required> </div> </div> <div class="form-group"> <div class="col-md-12"> <input type="tel" minlength="10" class="form-control" id="tel" name="tel" placeholder="Contact Number:*" value="<?php echo $send_data['tel']; ?>" required> </div> </div> <div class="form-group"> <div class="col-md-12"> <textarea class="form-control" id="message" name="message" placeholder="General Enquiry:" required><?php echo $send_data['message']; ?></textarea> </div> </div> <div class="form-group"> <div class=" col-md-12"> <button type="submit" class="button btn btn-warning">Send</button> </div> </div> </form> </div> </div> </div> </section>
<?php $target_page = false; $send_to = config('company_email'); $company_name = config('company_name'); $errors = array(); $posted = false; $success = false; $current_url = (isset($_POST['page']) && !empty($_POST['page'])) ? $_POST['page'] : 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; # is this the normal or quick form? $quick = (isset($_POST['type']) && $_POST['type'] == 'quick') ? true : false; # set up the common fields $send_data = array(); $send_data['name'] = (isset($_POST['name']) && !empty($_POST['name'])) ? $_POST['name'] : ''; $send_data['email'] = (isset($_POST['email']) && !empty($_POST['email'])) ? $_POST['email'] : ''; # if this is not the quick form then add the additional fields if (!$quick) { $send_data['tel'] = (isset($_POST['tel']) && !empty($_POST['tel'])) ? $_POST['tel'] : ''; } $send_data['message'] = (isset($_POST['message']) && !empty($_POST['message']) && $_POST['message'] != ' ') ? $_POST['message'] : ''; # check if the form has been submitted if (isset($_POST['bts']) && $_POST['bts'] == '') { # the form has been posted $posted = true; # check the name value if ($send_data['name'] == '') { $errors[] = 'Please fill in your name'; } # check the email value if ($send_data['email'] == '') { $errors[] = 'Please fill in your email address'; } else { # validate the email $validate_email = filter_var($send_data['email'], FILTER_VALIDATE_EMAIL); if (!$validate_email) { $errors[] = 'Please check your email address is correct'; } else { # check the domains MX records if(!checkdnsrr(array_pop(explode("@",$send_data['email'])),"MX")){ $errors[] = 'Please use a valid email address'; } } } # if this is not the quick form then check the additional fields if (!$quick) { if (strlen($send_data['tel']) < 10) { $errors[] = 'Please add a valid telephone number!'; } } # check the message if ($send_data['message'] == '') { $errors[] = 'Please fill in a message'; } # if there are no errors then send the message if (count($errors) == 0) { $send_from = $send_data['name'] . "<" . $send_data['email'] . ">"; $subject = 'Enquiry From Website ' . $_SERVER['HTTP_HOST']; $email_message = 'Below are the details that have been submitted on your contact form' . "\\n\\n"; $email_message .= '________________________________________' . "\\n\\n"; if (count($send_data) > 0) { foreach ($send_data as $key => $value) { $email_message .= $key . ' : ' . htmlspecialchars($value) . "\\n"; } } $email_message .= '________________________________________' . "\\n\\n"; $email_message .= 'IP : ' . $_SERVER["REMOTE_ADDR"] . "\\n\\n"; $email_message .= 'URL : ' . $current_url . "\\n\\n"; $email_message .= 'WUKmedia | http://wukmedia.uk'; #$headers = "From: " . strip_tags($send_from) . "\\r\\n"; #$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\\r\\n"; #$headers .= "MIME-Version: 1.0\\r\\n"; #$headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\\n"; if (mail($send_to, $subject, $email_message, "From: " . $send_from)) { $success = true; # now that the enquiry has been sent we can confirm to user $FromCompany = $company_name . "<" . $send_to . ">"; $thanks_email = "Thank you for your enquiry " . $send_data['name'] . ".\\n"; $thanks_email .= "We will be back in touch with you shortly, \\n\\n"; $thanks_email .= $company_name . "\\n"; $thanks_email .= 'http://' . $_SERVER['HTTP_HOST'] . "\\n\\n\\n"; mail($send_data['email'], "Thanks for the Enquiry", $thanks_email, "From: " . $send_from); # now store the data in a json array $send_data['ip'] = $_SERVER["REMOTE_ADDR"]; $send_data['timestamp'] = time(); $json_array = "\\n" . json_encode($send_data); $json_file = $_SERVER['DOCUMENT_ROOT'] . '/tp/enquiries/enquiries.json'; file_put_contents($json_file, $json_array, FILE_APPEND | LOCK_EX); } else { $errors[] = 'Your enquiry has not been sent, please try again'; } } }
$target_page = false;
$send_to = config('company_email');
$company_name = config('company_name');
$errors = array();
$posted = false;
$success = false;
$current_url = (isset($_POST['page']) && !empty($_POST['page'])) ? $_POST['page'] : 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
# is this the normal or quick form?
$quick = (isset($_POST['type']) && $_POST['type'] == 'quick') ? true : false;
# set up the common fields
$send_data = array();
$send_data['name'] = (isset($_POST['name']) && !empty($_POST['name'])) ? $_POST['name'] : '';
$send_data['email'] = (isset($_POST['email']) && !empty($_POST['email'])) ? $_POST['email'] : '';
# if this is not the quick form then add the additional fields
if (!$quick) {
$send_data['tel'] = (isset($_POST['tel']) && !empty($_POST['tel'])) ? $_POST['tel'] : '';
}
$send_data['message'] = (isset($_POST['message']) && !empty($_POST['message']) && $_POST['message'] != ' ') ? $_POST['message'] : '';
# check if the form has been submitted
if (isset($_POST['bts']) && $_POST['bts'] == '') {
# the form has been posted
$posted = true;
# check the name value
if ($send_data['name'] == '') {
$errors[] = 'Please fill in your name';
}
# check the email value
if ($send_data['email'] == '') {
$errors[] = 'Please fill in your email address';
} else {
# validate the email
$validate_email = filter_var($send_data['email'], FILTER_VALIDATE_EMAIL);
if (!$validate_email) {
$errors[] = 'Please check your email address is correct';
} else {
# check the domains MX records
if(!checkdnsrr(array_pop(explode("@",$send_data['email'])),"MX")){
$errors[] = 'Please use a valid email address';
}
}
}
# if this is not the quick form then check the additional fields
if (!$quick) {
if (strlen($send_data['tel']) < 10) {
$errors[] = 'Please add a valid telephone number!';
}
}
# check the message
if ($send_data['message'] == '') {
$errors[] = 'Please fill in a message';
}
# if there are no errors then send the message
if (count($errors) == 0) {
$send_from = $send_data['name'] . "<" . $send_data['email'] . ">";
$subject = 'Enquiry From Website ' . $_SERVER['HTTP_HOST'];
$email_message = 'Below are the details that have been submitted on your contact form' . "\n\n";
$email_message .= '________________________________________' . "\n\n";
if (count($send_data) > 0) {
foreach ($send_data as $key => $value) {
$email_message .= $key . ' : ' . htmlspecialchars($value) . "\n";
}
}
$email_message .= '________________________________________' . "\n\n";
$email_message .= 'IP : ' . $_SERVER["REMOTE_ADDR"] . "\n\n";
$email_message .= 'URL : ' . $current_url . "\n\n";
$email_message .= 'WUKmedia | http://wukmedia.uk';
#$headers = "From: " . strip_tags($send_from) . "\r\n";
#$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
#$headers .= "MIME-Version: 1.0\r\n";
#$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (mail($send_to, $subject, $email_message, "From: " . $send_from)) {
$success = true;
# now that the enquiry has been sent we can confirm to user
$FromCompany = $company_name . "<" . $send_to . ">";
$thanks_email = "Thank you for your enquiry " . $send_data['name'] . ".\n";
$thanks_email .= "We will be back in touch with you shortly, \n\n";
$thanks_email .= $company_name . "\n";
$thanks_email .= 'http://' . $_SERVER['HTTP_HOST'] . "\n\n\n";
mail($send_data['email'], "Thanks for the Enquiry", $thanks_email, "From: " . $send_from);
# now store the data in a json array
$send_data['ip'] = $_SERVER["REMOTE_ADDR"];
$send_data['timestamp'] = time();
$json_array = "\n" . json_encode($send_data);
$json_file = $_SERVER['DOCUMENT_ROOT'] . '/tp/enquiries/enquiries.json';
file_put_contents($json_file, $json_array, FILE_APPEND | LOCK_EX);
} else {
$errors[] = 'Your enquiry has not been sent, please try again';
}
}
}
只需在标签复选框中放置required
。 例如:
<input type="checkbox" name="" value="" required> text
验证我编写的表单时,我只使用Recaptcha。 这是您的选择吗? 如果是,则您需要该站点处于活动状态,以便您可以访问该站点的域名。
要开始此操作,请访问: https : //www.google.com/recaptcha/intro/v3beta.html
获取SITEKEY和SECERTKEY-他们逐步(非常简单)介绍了如何访问这两个密钥。
一旦有了它们,它们在head标签中只有一小段代码,如下所示:
<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
之后,只需将代码添加到表单标签中,如下所示:
<form>
<!-- put this wherever you want it in the form - usually at the bottom -->
<div class="g-recaptcha" data-sitekey="<!-- Site Key HERE -->"></div>
</form>
最后,只需在处理表单数据的PHP代码中授权它即可,例如:
$captcha = $_POST["g-recaptcha-response"]; // Declare variable
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
header("Location: <!-- Error HTML page OR echo and error statement -->");
exit;
}
$secretKey = "<!-- SECRET KEY HERE -->";
$ip = $_SERVER['REMOTE_ADDR'];
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response, true);
if(intval($responseKeys["success"]) !== 1) {
echo '<h2>Spam Detected</h2>';
}
else {
header("Location: <!-- Thank you HTML page OR echo and thanks statement -->");
}
您会看到我已评论该站点和密钥的去向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.