Reputation: 977
I'm trying to make a simple booking calendar using Eonasdan's bootstrap-datetimepicker which is built upon moment.js
library for which I included localization file. I set up a basic linked pickers input fields and a button to send an e-mail with the dates using simple ajax request. However, upon submitting the form, I get the following error:
// locale-hr.js (line 89, col 1)
TypeError: this is undefined switch (this.day()) {
Excerpt from the file (lines 88-102):
nextWeek : function () {
switch (this.day()) {
case 0:
return '[u] [nedjelju] [u] LT';
case 3:
return '[u] [srijedu] [u] LT';
case 6:
return '[u] [subotu] [u] LT';
case 1:
case 2:
case 4:
case 5:
return '[u] dddd [u] LT';
}
},
No e-mails are sent but dates are fetched from the calendar.
This is my ajax file:
$(function() {
$("#reservationForm").on("submit", function(e) {
e.preventDefault();
var url = "../bin/reservation.php";
// stores dates from each calendar
var date1 = $("#datetimepicker1").data("DateTimePicker").date();
var date2 = $("#datetimepicker2").data("DateTimePicker").date();
$.ajax({
type: "POST",
url: url,
data: { date1: date1, date2: date2 },
}).done(function(response) {
alert("Msg sent");
});
});
});
And the PHP file:
<?php
if ( empty($_POST['date1']) || empty($_POST['date2']) ) {
echo ERROR_NO_ARGS;
return false;
}
$date1 = $_POST['date1'];
$date2 = $_POST['date2'];
$to = '[email protected]';
$email_subject = "Title";
$email_body = "Selected dates:" . "From $date1 to $date2";
$headers = "From: Contact form\n";
$headers .= "Content-Type: text/plain; charset=utf-8" . "\r\n";
mail($to, $email_subject, $email_body, $headers);
return true;
?>
It's pretty basic stuff as you can see. I'd like to know what am I missing in here so that locale-hr.js has no object to reference or if I'm doing something wrong or if this is known issue that needs to be fixed (Google didn't offer me anything of sort). Here's jsfiddle but without ajax call as I never used fiddle before and don't know how to set it up at the moment. I might come back later to figure it out and add it.
If I should expand the question, please inform me. Thanks.
Upvotes: 2
Views: 1874
Reputation: 31482
Eonasdan's bootstrap-datetimepicker date
method returns a moment object, so you are trying to send to your server an object that jQuery cannot serialize (see jQuery.ajax
data
section). You have to change the type you send to your server, you can:
format
(if you need you can specify format). Your code will be: data: { date1: date1.format(), date2: date2.format() }
valueOf()
. Code: data: { date1: date1.valueOf(), date2: date2.valueOf() }
unix()
. Code: data: { date1: date1.unix(), date2: date2.unix() }
Upvotes: 1
Reputation: 1137
possible there are some bugs:
updated fiddle :
`https://jsfiddle.net/dssoft32/d4zuxy4e/2/`
// Template JS
var fnMain = function(){
// Calendar localization
var hr = moment.locale('hr', {
days: ["Ponedjeljak", "Utorak", "Srijeda", "Četvrtak", "Petak", "Subota","Nedjelja"],
weekdaysShort: ["Pon", "Uto", "Sri", "Čet", "Pet", "Sub","Ned"],
weekdaysMin: ["Po", "Ut", "Sr", "Če", "Pe", "Su", "Ne"],
months: ["Siječanj", "Veljača", "Ožujak", "Travanj", "Svibanj", "Lipanj", "Srpanj", "Kolovoz", "Rujan", "Listopad", "Studeni", "Prosinac"],
monthsShort: ["Sij", "Velj", "Ožu", "Tra", "Svi", "Lip", "Srp", "Kol", "Ruj", "Lis", "Stu", "Pro"],
today: "Danas",
}
);
var enumerateDaysBetweenDates = function( startDate, endDate ) {
var dates = [];
while (startDate<= endDate) {
dates.push(startDate.format('D/M/YYYY'));
startDate.add(1, 'days');
}
return dates;
};
var formatDisabledDates = function( dateSet ) {
var newSet = [];
for( var i=0; i < dateSet.length; i++ ) {
newSet.push( moment( dateSet[i], "D/M/YYYY") );
}
return newSet;
}
var fromDate = moment("10/07/2016", "D/M/YYYY");
var toDate = moment("21/07/2016", "D/M/YYYY");
var results = enumerateDaysBetweenDates(fromDate, toDate);
//});
var disabledSet = formatDisabledDates( results );
console.log( disabledSet );
var chckDate;
$('#datetimepicker1').datetimepicker({
useCurrent: false,
allowInputToggle: true,
viewMode: 'days',
//minDate: moment(),
locale: hr,
format: 'DD/MM/YYYY',
disabledDates: disabledSet
}).on("dp.change", function (e) {
chckDate = $(this).data("DateTimePicker").date();
chckDate = chckDate.format('DD. MM. YYYY');
}).on("dp.show", function (e){
$("[data-day*='"+ chckDate + "']").addClass("startFrom");
});
$('#datetimepicker2').datetimepicker({
useCurrent: false,
allowInputToggle: true,
viewMode: 'days',
locale: hr,
format: 'DD/MM/YYYY',
disabledDates: disabledSet
}).on("dp.change", function (e) {
//
}).on("dp.show", function (e){
$("[data-day*='"+ chckDate + "']").addClass("startFrom");
});
} // fnMain endDate
$( document ).ready( fnMain );
body {
background-color: #eee;
}
.input-group {
margin-top: 20px;
}
.bootstrap-datetimepicker-widget table td.disabled, .bootstrap-datetimepicker-widget table td.disabled:hover {
background: rgba(255, 0, 0, 0.1) none repeat scroll 0 0;
border-radius: 0;
color: #777;
cursor: not-allowed;
}
.bootstrap-datetimepicker-widget table td.startFrom, .bootstrap-datetimepicker-widget table td.startFrom:hover {
background-color: orange;
border-radius: 0;
color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
<script src="https://github.com/moment/moment/blob/develop/locale/hr.js"></script>
<div class="container">
<form id="reservationForm">
<div class='col-md-6'>
<!-- FROM -->
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<!-- TO -->
<div class='col-md-6'>
<div class="form-group">
<div class='input-group date' id='datetimepicker2'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<div class="col-md-12">
<button class="logdates" type="submit">Send</button>
</div>
</form>
</div>
Upvotes: 0