Reputation: 21
I am trying to add another validation to a contact form. I need a checkbox at the bottom of the form which the user check before they can submit the form.
This is what i have so far, which validates the form. I would like to know how to add a checkbox so the form is only submitted if the box is checked
<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';
}
}
}
Upvotes: 0
Views: 494
Reputation: 1921
When validating the forms I code, I just use the Recaptcha. Is this an option for you? If it is then you need the site to be live so you have access to the domain name of the site.
To begin this go to: https://www.google.com/recaptcha/intro/v3beta.html
Get a SITEKEY & SECERTKEY - they have step by step (very simple) on how to access these two keys.
Once you have them only a small snippet of code in the head tags like so:
<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
After, you just have to add the code into your form tags like so:
<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>
And then finally just authorise it in your PHP code that handles the form data, for example:
$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 -->");
}
You'll see I've commented where the site & secret key go.
Upvotes: 0
Reputation: 620
just put required
on tag checkbox. ex:
<input type="checkbox" name="" value="" required> text
Upvotes: 1