I am using Bootstrap to create a simple contact form and would like to achieve the following when pressing the "submit" button:
- Validate the form fields using jQuery
- Submit the form if validation passes, using PHP as the processor
- Show a message inside of a Bootstrap modal dialog that says the form has been emailed
Knowing that my PHP and JS knowledge is below average, I am glad to say that I have been able to make all items work separately, but I am not able to combine them...
Could you give me some input?
Validation uses :
var validator = $("#contact").validate({
...
PHP uses :
mail()
Both work when submitting the form with a button like this:
<button type="submit" name="submit" id="submitButton" class="btn btn-bcome pull-right">Send</button>
To activate the dialog, I changed the button to:
<button href="#submit" role="button" button type="submit" name="submit" id="submitButton" class="btn btn-bcome pull-right" data-toggle="modal">Send</button>
This makes the modal appear correctly, but validation and submission do not occur.
I have found a related post : jQuery Modal that appears on form submit. But I do not know to adapt this specifically to my case.
I have tried to adapt what you told me and this is what I got : Html part :
button href="#submit" role="button" button type="submit" name="submit" id="submitButton" class="btn btn-bcome pull-right" data-toggle="modal">Envoyer</button>
and
<div id="submit" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Thanks for your mail</h3>
</div>
<div class="modal-body">
<p>We will get back to you soon</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
In my JS file :
var $validator = {};
$(document).ready(function()
{
// validate signup form on keyup and submit
var validator = $("#contact").validate({
submitHandler: function(form)
{
//code to submit your form here
//code to open your modal here
errorClass:'error',
validClass:'success',
errorElement:'span',
highlight: function (element, errorClass, validClass) {
$(element).parents("div[class='clearfix']").addClass(errorClass).removeClass(validClass);
},
unhighlight: function (element, errorClass, validClass) {
$(element).parents(".error").removeClass(errorClass).addClass(validClass);
},
rules: {
fname: {
required: true,
minlength: 2
},
name: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
},
message: {
required: true,
minlength: 10
}
},
messages: {
fname: {
required: '<span class="help-inline">First name.</span>',
minlength: jQuery.format('<span class="help-inline">2 chars</span>')
},
name: {
required: '<span class="help-inline">Name.</span>',
minlength: jQuery.format('<span class="help-inline">2 chars</span>')
},
email: {
required: '<span class="help-inline">Email.</span>',
email: '<span class="help-inline">Ex : name@exemple.com</span>'
},
message: {
required: '<span class="help-block">Message</span>',
minlength: jQuery.format('<span class="help-block">10 chars</span>')
$('form').submit(function() {
if ($validator.numberOfInvalids() > 0) {
$('#submit').modal('hide');
} else {
$('#submit').modal('show');
}
}
});
}
);
I am surely missing something but for the moment, only the modal part is executed and not the validation part or submit form part.
Any idea what I should do to make this work?