php fullCalendar 事件 post 方法到 MySQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13338817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
fullCalendar events post method to MySQL
提问by SlartyBartfast
I am attempting to create a MySQL backed events interface, using fullCalendar and MySQL. I have tried to manipulate the examples in the fullCalendar documentation and have successfully created a events feed from my database.
我正在尝试使用 fullCalendar 和 MySQL 创建一个 MySQL 支持的事件界面。我尝试操作 fullCalendar 文档中的示例,并成功地从我的数据库创建了一个事件源。
I am now trying to create a eventDropcall which sends an events id, title and start time to the database. I used the code from a previous questionto create the eventDropcall, here is the JavaScript for the whole callendar page:
我现在正在尝试创建一个eventDrop调用,该调用将事件 ID、标题和开始时间发送到数据库。我使用上一个问题中的代码来创建eventDrop调用,这里是整个 callendar 页面的 JavaScript:
$(document).ready(function() {
/* initialize the external events
-----------------------------------------------------------------*/
$('#external-events div.external-event').each(function() {
// create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
// it doesn't need to have a start or end
var eventObject = {
title: $.trim($(this).text()) // use the element's text as the event title
};
// store the Event Object in the DOM element so we can get to it later
$(this).data('eventObject', eventObject);
// make the event draggable using jQuery UI
$(this).draggable({
zIndex: 999,
revert: true, // will cause the event to go back to its
revertDuration: 0 // original position after the drag
});
});
/* initialize the calendar
-----------------------------------------------------------------*/
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
droppable: true, // this allows things to be dropped onto the calendar !!!
drop: function(date, allDay) { // this function is called when something is dropped
// retrieve the dropped element's stored Event Object
var originalEventObject = $(this).data('eventObject');
// we need to copy it, so that multiple events don't have a reference to the same object
var copiedEventObject = $.extend({}, originalEventObject);
// assign it the date that was reported
copiedEventObject.start = date;
copiedEventObject.allDay = allDay;
// render the event on the calendar
// the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
$('#calendar').fullCalendar('renderEvent', copiedEventObject, true);
// if so, remove the element from the "Draggable Events" list
$(this).remove();
},
// events from mysql database
events: "/json-events.php",
// submit to database
eventDrop: function(calEvent, jsEvent, view) {
var method = 'POST';
var path = 'submit.php';
var params = new Array();
params['id'] = calEvent.id;
params['start'] = calEvent.start;
params['end'] = calEvent.end;
params['title'] = calEvent.title;
post_to_url( path, params, method);
}
});
});
The PHP file I hoped would receive the POST data and insert it into the database with an end time equal to the start time plus 15 mins (edited after answer below):
我希望的 PHP 文件会接收 POST 数据并将其插入到数据库中,结束时间等于开始时间加 15 分钟(在下面的回答后编辑):
<?php
mysql_connect("") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$id = $_POST["id"];
$title = $_POST["title"];
$start = $_POST["start"];
$end = date(Y-m-d T H:i:s , strtotime($start)+900);
$query = "INSERT INTO `events` VALUES (`$id`, `$title`, `$start`, `$end`, ``)";
mysql_query($query);
print $query;
?>
The database is not receiving the event data.
数据库未接收事件数据。
回答by Daniel Sloan Johnson
This is the conclusion I have come up with and I have no problem running this off my test and public server. I have taken the FullCalendar and this is the format I use.
这是我得出的结论,在我的测试和公共服务器上运行它没有问题。我采用了 FullCalendar,这是我使用的格式。
The database is real simple.
数据库真的很简单。
id integer 11 chars primary key auto-increment,
title varchar 50,
start varchar 50,
end varchar 50,
url varchar 50.
This is the index.phpor index.htmlfile.
这是index.php或index.html文件。
<!DOCTYPE html>
<html>
<head>
<link href='css/fullcalendar.css' rel='stylesheet' />
<link href='css/fullcalendar.print.css' rel='stylesheet' media='print' />
<script src='js/jquery-1.9.1.min.js'></script>
<script src='js/jquery-ui-1.10.2.custom.min.js'></script>
<script src='js/fullcalendar.min.js'></script>
<script>
$(document).ready(function() {
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
events: "json.php",
eventDrop: function(event, delta) {
alert(event.title + ' was moved ' + delta + ' days\n' +
'(should probably update your database)');
},
loading: function(bool) {
if (bool) $('#loading').show();
else $('#loading').hide();
}
});
});
</script>
<style>
body {
margin-top: 40px;
text-align: center;
font-size: 14px;
font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
}
#loading {
position: absolute;
top: 5px;
right: 5px;
}
#calendar {
width: 900px;
margin: 0 auto;
}
</style>
</head>
<body>
<div id='loading' style='display:none'>loading...</div>
<div id='calendar'></div>
<p>json.php needs to be running in the same directory.</p>
</body>
</html>
This is the json.phpfile.
这是json.php文件。
<?php
mysql_pconnect("localhost", "root", "") or die("Could not connect");
mysql_select_db("calendar") or die("Could not select database");
$rs = mysql_query("SELECT * FROM events ORDER BY start ASC");
$arr = array();
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
echo json_encode($arr);
?>
回答by Srihari Goud
Once remove the column names in insert query and try, if you don't get then let's think about it. Do something like this
一旦删除插入查询中的列名并尝试,如果你没有得到那么让我们考虑一下。做这样的事情
mysql_query(INSERT INTO `events` VALUES ('$id', '$title', '$start', '$end', ''));
Just print the post values before inserting, if it seems clear, then check your query.
只需在插入之前打印帖子值,如果看起来很清楚,请检查您的查询。
echo the query and exit, you may get something like this
回显查询并退出,你可能会得到这样的东西
mysql_query(INSERT INTO `events` VALUES ('23', 'event title', 'start date', 'end date', ''));
Run this query to find errors if any
运行此查询以查找错误(如果有)
回答by marc
print $query- there should be a ;at the end
print $query-应该有一个;末

