JS 全日历更新数据库
JS Fullcalendar Update Database
我正在使用完整的日历 js,我可以使用 phpmyadmin 将创建的事件连接到数据库中,但是它不会更新数据库。这是代码:
<link href='css/fullcalendar.css' rel='stylesheet' />
<script src='js/moment.min.js'></script>
<script src='js/jquery.min.js'></script>
<script src='js/fullcalendar.min.js'></script>
<script>
$(document).ready(function() {
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var calendar = $('#calendar').fullCalendar({
editable: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
events: "calendarevents.php",
// Convert the allDay from string to boolean
eventRender: function(event, element, view) {
if (event.allDay === 'true') {
event.allDay = true;
} else {
event.allDay = false;
}
},
selectable: true,
selectHelper: true,
select: function(start, end, allDay) {
var title = prompt('Event Title:');
var url = prompt('Type Event url, if exits:');
if (title) {
var start = $.fullCalendar.moment(start).format();
var end = $.fullCalendar.moment(end).format();
$.ajax({
url: 'calendaraddevents.php',
data: 'title='+ title+'&start='+ start +'&end='+ end +'&url='+ url ,
type: "POST",
success: function(json) {
alert('Added Successfully');
}
});
calendar.fullCalendar('renderEvent',
{
title: title,
start: start,
end: end,
allDay: allDay
},
true // make the event "stick"
);
}
calendar.fullCalendar('unselect');
},
editable: true,
eventDrop: function(event, delta) {
var start = $.fullCalendar.moment(start).format();
var end = $.fullCalendar.moment(end).format();
$.ajax({
url: 'calendarupdateevents.php',
data: 'title=' + event.title + '&start='+ start +'&end=' + end + '&id=' + event.id ,
type: "POST",
success: function(json) {
alert("Updated Successfully");
}
});
},
eventResize: function(event) {
var start = $.fullCalendar.moment(start).format();
var end = $.fullCalendar.moment(end).format();
$.ajax({
url: 'calendarupdateevents.php',
data: 'title=' + event.title + '&start=' + start + '&end=' + end + '&id=' + event.id ,
type: "POST",
success: function(json) {
alert("Updated Successfully");
}
});
}
});
});
</script>
这是 calendarupdateevents.php
的代码
<?php
/* Values received via ajax */
$id = $_POST['id'];
$title = $_POST['title'];
$start = $_POST['start'];
$end = $_POST['end'];
// connection to the database
try {
$bdd = new PDO('mysql:host=localhost;dbname=ththy', 'calendar', '1234');
} catch(Exception $e) {
exit('Unable to connect to database.');
}
// update the records
$sql = "UPDATE evenement SET title=?, start=?, end=? WHERE id=?";
$q = $bdd->prepare($sql);
$q->execute();
?>
正如 halfer 所说,您没有向查询传递任何参数。
试试这个:
<?php
/* Values received via ajax */
$values = array(
'id' => int($_POST['id']),
'title' => htmlentities($_POST['title'], ENT_QUOTES | ENT_IGNORE, "UTF-8"),
'start' => htmlentities($_POST['start'], ENT_QUOTES | ENT_IGNORE, "UTF-8"),
'end' => htmlentities($_POST['end'], ENT_QUOTES | ENT_IGNORE, "UTF-8")
);
// connection to the database
try {
$bdd = new PDO('mysql:host=localhost;dbname=ththy', 'calendar', '1234');
} catch(Exception $e) {
exit('Unable to connect to database.');
}
// update the records
$sql = "UPDATE evenement SET title=?, start=?, end=? WHERE id=?";
$q = $bdd->prepare($sql);
$q->execute($values);
?>
示例不在 return事件的正确范围内。对于return活动起止时刻,我建议:
var start = event.start.format();
var end = event.end.format();
我正在使用完整的日历 js,我可以使用 phpmyadmin 将创建的事件连接到数据库中,但是它不会更新数据库。这是代码:
<link href='css/fullcalendar.css' rel='stylesheet' />
<script src='js/moment.min.js'></script>
<script src='js/jquery.min.js'></script>
<script src='js/fullcalendar.min.js'></script>
<script>
$(document).ready(function() {
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var calendar = $('#calendar').fullCalendar({
editable: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
events: "calendarevents.php",
// Convert the allDay from string to boolean
eventRender: function(event, element, view) {
if (event.allDay === 'true') {
event.allDay = true;
} else {
event.allDay = false;
}
},
selectable: true,
selectHelper: true,
select: function(start, end, allDay) {
var title = prompt('Event Title:');
var url = prompt('Type Event url, if exits:');
if (title) {
var start = $.fullCalendar.moment(start).format();
var end = $.fullCalendar.moment(end).format();
$.ajax({
url: 'calendaraddevents.php',
data: 'title='+ title+'&start='+ start +'&end='+ end +'&url='+ url ,
type: "POST",
success: function(json) {
alert('Added Successfully');
}
});
calendar.fullCalendar('renderEvent',
{
title: title,
start: start,
end: end,
allDay: allDay
},
true // make the event "stick"
);
}
calendar.fullCalendar('unselect');
},
editable: true,
eventDrop: function(event, delta) {
var start = $.fullCalendar.moment(start).format();
var end = $.fullCalendar.moment(end).format();
$.ajax({
url: 'calendarupdateevents.php',
data: 'title=' + event.title + '&start='+ start +'&end=' + end + '&id=' + event.id ,
type: "POST",
success: function(json) {
alert("Updated Successfully");
}
});
},
eventResize: function(event) {
var start = $.fullCalendar.moment(start).format();
var end = $.fullCalendar.moment(end).format();
$.ajax({
url: 'calendarupdateevents.php',
data: 'title=' + event.title + '&start=' + start + '&end=' + end + '&id=' + event.id ,
type: "POST",
success: function(json) {
alert("Updated Successfully");
}
});
}
});
});
</script>
这是 calendarupdateevents.php
的代码<?php
/* Values received via ajax */
$id = $_POST['id'];
$title = $_POST['title'];
$start = $_POST['start'];
$end = $_POST['end'];
// connection to the database
try {
$bdd = new PDO('mysql:host=localhost;dbname=ththy', 'calendar', '1234');
} catch(Exception $e) {
exit('Unable to connect to database.');
}
// update the records
$sql = "UPDATE evenement SET title=?, start=?, end=? WHERE id=?";
$q = $bdd->prepare($sql);
$q->execute();
?>
正如 halfer 所说,您没有向查询传递任何参数。 试试这个:
<?php
/* Values received via ajax */
$values = array(
'id' => int($_POST['id']),
'title' => htmlentities($_POST['title'], ENT_QUOTES | ENT_IGNORE, "UTF-8"),
'start' => htmlentities($_POST['start'], ENT_QUOTES | ENT_IGNORE, "UTF-8"),
'end' => htmlentities($_POST['end'], ENT_QUOTES | ENT_IGNORE, "UTF-8")
);
// connection to the database
try {
$bdd = new PDO('mysql:host=localhost;dbname=ththy', 'calendar', '1234');
} catch(Exception $e) {
exit('Unable to connect to database.');
}
// update the records
$sql = "UPDATE evenement SET title=?, start=?, end=? WHERE id=?";
$q = $bdd->prepare($sql);
$q->execute($values);
?>
示例不在 return事件的正确范围内。对于return活动起止时刻,我建议:
var start = event.start.format();
var end = event.end.format();