I am trying to add a checkbox to my simple contact form. I want this to be validated so it checks before being able to send the form. I would really appreciate some help.
FORM:
<form id="contactForm" name="sentMessage" novalidate="">
<div class="control-group form-group">
<div class="controls"><label>Navn:</label> <input id="name" class="form-control" required="" type="text" data-validation-required-message="Skriv inn ditt navn." />
<p class="help-block"> </p>
</div>
</div>
<div class="control-group form-group">
<div class="controls"><label>Telefonnummer:</label> <input id="phone" class="form-control" required="" type="tel" data-validation-required-message="Skriv inn et telefonnummer." />
<div class="help-block"> </div>
</div>
</div>
<div class="control-group form-group">
<div class="controls"><label>E-post:</label> <input id="email" class="form-control" required="" type="email" data-validation-required-message="Skriv inn en e-postadresse." />
<div class="help-block"> </div>
</div>
</div>
<div class="control-group form-group">
<div class="controls"><label>Melding:</label> <textarea id="message" class="form-control" style="resize: none;" cols="100" maxlength="999" required="" rows="10" data-validation-required-message="Skriv en melding"></textarea>
<div class="help-block"> </div>
</div>
</div>
CHECKBOX IS WANTED HERE
<div id="success"> </div>
<!-- For success/fail messages --> <button id="sendMessageButton" class="btn btn-success" type="submit">Send oss melding</button></form>
contact_me.js:
$(function() {
$("#contactForm input,#contactForm textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'Kontaktskjema',
formId: 'contactForm'
});
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var message = $("textarea#message").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$this = $("#sendMessageButton");
$this.prop("disabled", true); // Disable submit button until AJAX call is complete to prevent duplicate messages
$.ajax({
url: "/mail/contact_me.php",
type: "POST",
data: {
name: name,
phone: phone,
email: email,
message: message
},
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Takk, meldingen er sendt. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append($("<strong>").text("Beklager " + firstName + ", noe gikk feil med sendingen. Prøv igjen senere!"));
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
complete: function() {
setTimeout(function() {
$this.prop("disabled", false); // Re-enable submit button when AJAX call is complete
}, 1000);
}
});
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
$('#success').html('');
});
contact_me.php:
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = 'email@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Kontaktskjema domain.com: $name";
$email_body = "Du har mottatt en melding fra kontaktskjemaet paa nettsiden domain.com.\n\n"."Her er meldingen:\n\nNavn: $name\n\nE-post: $email_address\n\nTelefon: $phone\n\nMelding:\n$message";
$headers = "From: noreply@domain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
The form is working fine now, but whatever I try to validate, if a checkbox is checked it stops working. I have searched and tried all the solutions I could find.
Aucun commentaire:
Enregistrer un commentaire