0

I am trying to save an event via ajax on the full calendar.

I have tested the php for inserting the events and it is working fine. So I realized that the events are not being passed via ajax.

They are not being passed for insertion or updatation.

<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>

Here is the insertion code (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));

And here is the update_events for updating the event :

 /* 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));

I would appreciate if someone could point me into understanding how to get the POST in the Ajax working or what I am doing wrong.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • possible duplicate of [When to use single quotes, double quotes, and backticks?](http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks) – Funk Forty Niner Mar 03 '16 at 13:16
  • 1
    if the problem isn't in JS/Ajax, then your question is a duplicate of that one ^ - you're using the wrong identifier qualifiers in the UPDATE portion. – Funk Forty Niner Mar 03 '16 at 13:17
  • *Eagle-eye Ralph!!* There should definitely be back ticks in that second query @Fred-ii-. OP how are you verifying that your post values are set? – Jay Blanchard Mar 03 '16 at 13:32
  • 1
    *Nobody seems to be listening to me Sam* @JayBlanchard but *you* spotted me ;-) – Funk Forty Niner Mar 03 '16 at 13:36
  • The insert and update are ok, the ajax is not working. –  Mar 03 '16 at 13:49
  • *"and update are ok"* - I sincerely doubt that, seeing the quotes around the column names. As per what you posted for your code that is. If you are indeed using ticks around those instead of regular quotes but failed to do so, then my answer is worth nothing now @UmarAftab – Funk Forty Niner Mar 03 '16 at 14:02

2 Answers2

1

As I stated in comments, you're using the incorrect identifer qualifiers for the columns in your UPDATE, being regular quotes ' rather than ticks.

Reference:

This is what you have

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

and this is what it should read as

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

and checking for errors for this, would have thrown you something about it.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Its still does not save the event to the database. –  Mar 03 '16 at 13:48
  • The insert is working fine, the ajax is not working. –  Mar 03 '16 at 13:49
  • @UmarAftab your ajax is related to the UPDATE, I wasn't referencing the INSERT. – Funk Forty Niner Mar 03 '16 at 13:50
  • Have you watched the AJAX request / response in the browser's console @UmarAftab? – Jay Blanchard Mar 03 '16 at 13:51
  • @JayBlanchard wondering if he even loaded jQuery *in the first place Sam* – Funk Forty Niner Mar 03 '16 at 14:04
  • *Yeah, it seems likely Ralph.* – Jay Blanchard Mar 03 '16 at 14:05
  • @UmarAftab Jay asked you a question about looking at your browser's console and we also wondering if you indeed loaded jQuery and in the right spot, or you may be using a deprecated version of jQuery, who knows. You just dropped off the face of the earth here. – Funk Forty Niner Mar 09 '16 at 15:56
  • It does load but the add_events.php file shows a Internal Server 500 xhr error. –  Mar 09 '16 at 15:58
  • *Sure is Ralph. Sure is.* @Fred-ii- – Jay Blanchard Mar 09 '16 at 15:59
  • @UmarAftab It's a server error. Read this Q&A on Stack http://stackoverflow.com/questions/4789613/ajax-500-internal-server-error and check your error logs. and http://stackoverflow.com/questions/19265914/500-internal-server-error-on-xhr-request and keep Googling that error message. – Funk Forty Niner Mar 09 '16 at 15:59
  • But that is gone now and it says xhr finished loading POST and the parameters. –  Mar 09 '16 at 16:06
  • @UmarAftab https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html *"**200 OK** The request has succeeded. The information returned with the response is dependent on the method used in the request, for example:...."* – Funk Forty Niner Mar 09 '16 at 16:08
  • Than what is preventing the post from inserting into the database. Because, when I test it directly without jQuery being involved it does register the events in the database. –  Mar 09 '16 at 16:11
  • @UmarAftab http://php.net/manual/en/pdo.error-handling.php - http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Mar 09 '16 at 16:12
0

You need to change the way you are sending data with the post ajax. Try this.

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

etc.

Vasil Rashkov
  • 1,818
  • 15
  • 27