Reputation: 1285
I am using FullCalendar and attempting to update an event start time when a user drags and drops an event in my calendar. This is the code I am using attached to the eventDrop callback (https://fullcalendar.io/docs/eventDrop):
alert(info.event.id + ' was dropped on ' + info.event.start);
$.ajax({
url: '/Post/dropPost',
type: 'POST',
data: { 'postSchedule': info.event.start, 'postId' : info.event.id },
});
},
This works in as much as the alert shows the correct event ID and the correct start date (in the following format: Tue May 05 2020 04:36:00). The '/Post/dropPost' method is also called and the postId variable is passed fine.
However, when it updates my database the postSchedule always updates as "00:00:00 00:00:00".
This is my dropPost method:
public function dropPost()
{
$data=[];
$postSchedule = $_POST['postSchedule'];
$postId = $_POST['postId'];
$droppedPost = new PostModel;
$data['id'] = $postId;
$data['postSchedule'] = $postSchedule;
$droppedPost->save($data);
}
From reading the FullCalendar documents my understand is that that dates should always be ISO8601 standard: https://fullcalendar.io/docs/date-parsing. So why does it not translate into my database.
The postSchedule column in my database is type DateTime.
Upvotes: 0
Views: 454
Reputation: 1961
The issue, I think is that you're not converting the postSchedule
in correct format to be stored in the database
.
Controller
First, check that the data is being retrieved by print_r($this->input->post())
.
If yes, then convert the data in correct form ie $data['postSchedule'] = date("Y-m-d H:i:s", strtotime($postSchedule));
and then store the data in database
with type DATETIME
.
Hope this helps you.
Upvotes: 0
Reputation: 1285
I finally found the answer. I needed to use .toISOString() on the info.event.start property.
Working code is as follows:
eventDrop: function(info) {
alert(info.event.id + ' was dropped on ' + info.event.start);
$.ajax({
url: '/Post/dropPost',
type: 'POST',
data: { 'postSchedule': info.event.start.toISOString(), 'postId' : info.event.id },
});
},
Upvotes: 1