发布不在Ajax中工作以保存数据库的输入

时间:2016-03-03 13:14:54

标签: javascript php jquery mysql ajax

我正在尝试通过完整日历上的ajax保存活动。

我已经测试了用于插入事件的php,它运行正常。所以我意识到事件不是通过ajax传递的。

它们不会被传递以进行插入或更新。

<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: "events.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);
    var end = $.fullCalendar.moment(end);
  $.ajax({
     url: 'add_events.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(event.start);
 var end = $.fullCalendar.moment(event.end);
 $.ajax({
   url: 'update_events.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(event.start);
 var end = $.fullCalendar.moment(event.end);
  $.ajax({
   url: 'update_events.php',
   data: 'title='+ event.title+'&start='+ start +'&end='+ end +'&id='+ event.id ,
  type: "POST",
  success: function(json) {
  alert("Updated Successfully");
  }
 });

 }

 });

});

</script>

这是插入代码(add_events.php)

 $title = $_POST['title'];
 $start = $_POST['start'];
 $end = $_POST['end'];
 $url = $_POST['url'];


// connection to the database
try {
  $bdd = new PDO('mysql:host=localhost;dbname=fullcalendar', 'umar', 'Umar@295');
 } catch(Exception $e) {
   exit('Unable to connect to database.');
 }

 // insert the records
   $sql = "INSERT INTO evenement (title, start, end, url) VALUES (:title, :start, :end, :url)";

   $q = $bdd->prepare($sql);
   $q->execute(array(':title'=>$title, ':start'=>$start, ':end'=>$end,  ':url'=>$url));

以下是更新事件的update_events:

 /* 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=fullcalendar', 'umar', 'Umar@295');
  } 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(array($title,$start,$end,$id));

如果有人能够让我了解如何在Ajax中运行POST或我做错了什么,我将不胜感激。

2 个答案:

答案 0 :(得分:2)

正如我在评论中所述,您在UPDATE中使用了不正确的标识符限定符,即常规引号File.Open而不是刻度。

参考:

这就是你拥有的

'

这应该是

$sql = "UPDATE `evenement` SET 'title'=?, 'start'=?, 'end'=? WHERE 'id'=?";

并为此检查错误,会让你有所了解。

答案 1 :(得分:0)

您需要使用post ajax更改发送数据的方式。 试试这个。

data: {
   title : event.title,
   start : start
   end : end,
   id : event.id
},