I am trying to adapt this Bootstrap and JQuery form from Mikhail Niedre. The form is posted here: https://codepen.io/webcane/pen/LBspI. Basicly it contains a contact form as below. This is working as expected:
<form class="form-horizontal callmeform" id="contact_form">
<fieldset>
<!-- Text input-->
<div class="form-group">
<label class="col-md-3 control-label" for="name">Name</label>
<div class="col-md-6">
<input id="name" name="name" type="text" placeholder="Name" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-3 control-label" for="name">Email</label>
<div class="col-md-6">
<input id="email" name="email" type="text" placeholder="Name" class="form-control input-md">
</div>
</div>
<p> </p>
<!-- Button -->
<div class="form-group">
<label class="col-md-3 control-label" for="submit"></label>
<div class="col-md-6">
<button id="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
<script>
jQuery(function ($) {
$('#contact_form').submit(function () {
var name = $('#name').val();
var msg = $('#name').val() + $('#options').val();
$.ajax({
type: 'POST',
url: 'https://mandrillapp.com/api/1.0/messages/send.json',
data: {
'key': 'xxx',
'message': {
'from_email': 'info@mail.com',
'from_name': 'From',
'headers': { 'Reply-To': email },
'subject': 'Subject name',
'text': msg,
'to': [{
'email': 'info@mail.com',
'name': 'Fill name here',
'type': 'to'
}]
}
}
}).done(function (response) {
$('#name').val('');
$('#email').val('');
window.location.href = '/thankyou';
}).fail(function (response) {
alert('Error sending message.');
});
return false;
});
}); </script>
I added a view select boxes i want to mail using this form. Somehow I can not get this to work. The response in the mail is 'undefined'. Maybe you can give me some tips what am I doing wrong. Here is the code I have so far. I am trying to get the values from the options in the mail:
<form class="form-horizontal callmeform" id="contact_form">
<fieldset>
<!-- Text input-->
<div class="form-group">
<label class="col-md-3 control-label" for="name">Naam *</label>
<div class="col-md-6">
<input id="name" name="name" type="text" placeholder="Naam" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-3 control-label" for="phone">E-Mail</label>
<div class="col-md-6">
<input id="email" name="email" type="text" placeholder="Uw E-mail adres" class="form-control input-md">
</div>
</div>
<div class="input-box">
<ul id="options-1-list" class="options-list">
<li><input type="checkbox" class="checkbox product-custom-option" name="options[]" id="options_1_1" value="1" /><span class="label"><label for="options_1_2">option 1 </label></span></li>
<li><input type="checkbox" class="checkbox product-custom-option" name="options[]" id="options_1_2" value="2" /><span class="label"><label for="options_1_3">option 2 </label></span></li>
<li><input type="checkbox" class="checkbox product-custom-option" name="options[]" id="options_1_3" value="3" /><span class="label"><label for="options_1_4">option 3</label></span></li></ul>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-3 control-label" for="submit"></label>
<div class="col-md-6">
<button id="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
<script>
jQuery(function ($) {
$('#contact_form').submit(function () {
var email = $('#email').val();
var name = $('#name').val();
var options = $("input[name='options[]']:checked");
var msg = $('#name').val() + '\n' + $('#email').val() + '\n' + $('#options').val();
$.ajax({
type: 'POST',
url: 'https://mandrillapp.com/api/1.0/messages/send.json',
data: {
'key': 'xxx',
'message': {
'from_email': 'info@mail.com',
'from_name': 'name',
'headers': { 'Reply-To': email },
'subject': 'Subject name',
'text': msg,
'to': [{
'email': 'info@mail.com',
'name': 'Name goes here',
'type': 'to'
}]
}
}
}).done(function (response) {
$('#name').val('');
$('#email').val('');
var values = new Array();
$.each($("input[name='options[]']:checked"), function() {
values.push($(this).val());
window.location.href = '/thankyou';
}).fail(function (response) {
alert('Error sending message.');
});
return false;
});
});
</script>
Working version:
<form class="form-horizontal callmeform" id="contact_form">
<fieldset>
<!-- Text input-->
<div class="form-group">
<label class="col-md-3 control-label" for="name">Naam *</label>
<div class="col-md-6">
<input id="name" name="name" type="text" placeholder="Naam" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-3 control-label" for="phone">E-Mail</label>
<div class="col-md-6">
<input id="email" name="email" type="text" placeholder="Uw E-mail adres" class="form-control input-md">
</div>
</div>
<div class="input-box">
<ul id="options-1-list" class="options-list">
<li><input type="checkbox" class="checkbox product-custom-option" name="options[]" id="options_1_1" value="1" /><span class="label"><label for="options_1_2">option 1 </label></span></li>
<li><input type="checkbox" class="checkbox product-custom-option" name="options[]" id="options_1_2" value="2" /><span class="label"><label for="options_1_3">option 2 </label></span></li>
<li><input type="checkbox" class="checkbox product-custom-option" name="options[]" id="options_1_3" value="3" /><span class="label"><label for="options_1_4">option 3</label></span></li></ul>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-3 control-label" for="submit"></label>
<div class="col-md-6">
<button id="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
<script>
jQuery(function ($) {
$('#contact_form').submit(function () {
var email = $('#email').val();
var name = $('#name').val();
var options = $("input[name='options[]']:checked");
var values = [];
$.each($("input[name='options[]']:checked"), function() {
values += $(this).val()+', ';
});
var msg = $('#name').val() + '\n' + $('#email').val() + '\n' + values;
$.ajax({
type: 'POST',
url: 'https://mandrillapp.com/api/1.0/messages/send.json',
data: {
'key': 'xxx',
'message': {
'from_email': 'info@mail.com',
'from_name': 'name',
'headers': { 'Reply-To': email },
'subject': 'Subject name',
'text': msg,
'to': [{
'email': 'info@mail.com',
'name': 'Name goes here',
'type': 'to'
}]
}
}
}).done(function (response) {
$('#name').val('');
$('#email').val('');
$('#msg').val('');
var values = new Array();
$.each($("input[name='options[]']:checked"), function() {
values.push($(this).val());
});
window.location.href = '/thankyou';
}).fail(function (response) {
alert('Error sending message.');
});
return false;
});
});
</script>
$('#options').val(); // this will not work.
Check this :- Select values of checkbox group with jQuery
Edit:
jQuery(function($) {
$('#contact_form').submit(function(ev) {
ev.preventDefault();
var email = $('#email').val();
var name = $('#name').val();
var options = $("input[name='options[]']:checked");
var values = '';
$.each($("input[name='options[]']:checked"), function() {
values += $(this).val()+', ';
});
var msg = $('#name').val() + '\n' + $('#email').val() + '\n' + values;
console.log(msg);
$.ajax({
type: 'POST',
url: 'https://mandrillapp.com/api/1.0/messages/send.json',
data: {
'key': 'xxx',
'message': {
'from_email': 'info@mail.com',
'from_name': 'name',
'headers': { 'Reply-To': email },
'subject': 'Subject name',
'text': msg,
'to': [{
'email': 'info@mail.com',
'name': 'Name goes here',
'type': 'to'
}]
}
}
}).done(function (response) {
$('#name').val('');
$('#email').val('');
var values = new Array();
$.each($("input[name='options[]']:checked"), function() {
values.push($(this).val());
window.location.href = '/thankyou';
}).fail(function (response) {
alert('Error sending message.');
});
return false;
});
});
Note: I haven't checked the ajax part but this will solve the undefined issue.