search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

Event creation does not work

Asked by Martin Teefy
3 years ago.

Hi,

I took the download for PHP usage via codeproject website and copied the files into my Xampp htdocs directory and changed the code to use MySQL instead but noticed the create php did not work / get called.

So I then created a folder to use within xampp/localhost using the original files from the download i.e. using Sqlite to find the display, move and delete code works but the event creation stills doesn't get called?

Thanks
Martin

Comment posted by Dan Letecky [DayPilot]
3 years ago.

The onTimeRangeSelected event handler in index.html has to be replaced with this:

  dp.onTimeRangeSelected = function (args) {
    var name = prompt("New event name:", "Event");
    dp.clearSelection();
    if (!name) return;
    dp.events.add(new DayPilot.Event({
      start: args.start,
      end: args.end,
      id: DayPilot.guid(),
      resource: args.resource,
      text: name
    }));

    DayPilot.Http.ajax({
      url: "backend_create.php",
      data: {
        start: args.start,
        end: args.end,
        text: name
      },
      success: function () {
        console.log("Created.");
      }
    });
  };

The sample project will be updated soon. Thanks for reporting the issue.

Answer posted by Dan Letecky [DayPilot]
3 years ago.

The tutorial is now updated and it uses the following logic:

dp.onTimeRangeSelected = function (args) {
  var name = prompt("New event name:", "Event")
  dp.clearSelection();
  if (!name) {
    return;
  }

  DayPilot.Http.ajax({
    url: "backend_create.php",
    data: {
      start: args.start,
      end: args.end,
      text: name
    },
    success: function (ajax) {
      var data = ajax.data;
      dp.events.add(new DayPilot.Event({
        start: args.start,
        end: args.end,
        id: data.id,
        text: name
      }));
      console.log("Created.");
    }
  });
};

Note that the event is now added to the Calendar when the HTTP call is completed and it uses the correct ID returned by the server.

This requires an updated backend_create.php (it needs to return the ID generated by the DB):

<?php
require_once '_db.php';

$json = file_get_contents('php://input');
$params = json_decode($json);

$insert = "INSERT INTO events (name, start, end) VALUES (:name, :start, :end)";

$stmt = $db->prepare($insert);

$stmt->bindParam(':start', $params->start);
$stmt->bindParam(':end', $params->end);
$stmt->bindParam(':name', $params->text);
$stmt->execute();

class Result {}

$response = new Result();
$response->result = 'OK';
$response->message = 'Created with id: '.$db->lastInsertId();
$response->id = $db->lastInsertId();

header('Content-Type: application/json');
echo json_encode($response);
Comment posted by Martin Teefy
3 years ago.

Hi,

Thanks for the swift reply.

I confirm this works with the sqlite code.

I will apply the change to my MySQL code.

Thanks
Martin

Comment posted by Dan Letecky [DayPilot]
3 years ago.

Great, thanks for the update, Martin!

Comment posted by Martin Teefy
3 years ago.

Dan,

I was wondering if you have an example of the event retrieval working against MySQL?

My adapted code adds events to MySQL but it never shows the events?

Checking the apache logs i saw an error:

Uncaught Error: Cannot use object of type stdClass as array in ....

So I changed the code from $row{'id'] to $row->id now I get no errors but still no events showing?

My code is following maybe you could spot my mistake?

Thanks
Martin

<?php
session_start();
error_reporting(0);
include('includes/config.php');

//$sql = "SELECT id,EventName,EventStartDate,EventEndDate FROM tblcalendarevents WHERE NOT ((EventEndDate <= :start) OR (EventStartDate >= :end))";
$sql = "SELECT * FROM tblcalendarevents";
$query = $dbh->prepare($sql);
//$query->bindParam(':start',$_GET['start'],PDO::PARAM_STR);
//$query->bindParam(':end',$_GET['end'],PDO::PARAM_STR);
$query->execute();

$results = $query->fetchAll();
//$results=$query->fetchAll(PDO::FETCH_OBJ);

echo $query->rowCount();

class Event {}
$events = array();

if($query->rowCount() > 0)
{
foreach($results as $row)
{

// echo $row['id'];

$e = new Event();
$e->id = $row['id'];
$e->text = $row['EventName'];
$e->start = $row['EventStartDate'];
$e->end = $row['EventEndDate'];
// $e->id = $row->id;
// $e->text = $row->EventName;
// $e->start = $row->EventStartDate;
// $e->end = $row->EventEndDate;
$events[] = $e;
}

print_r ($events);
}
else
{
echo "No data found";
}

header('Content-Type: application/json');
echo json_encode($events);

Comment posted by Dan Letecky [DayPilot]
3 years ago.

Martin,

You can take a look at this tutorial:
https://code.daypilot.org/27988/html5-calendar-with-day-week-month-views-javascript-php

It's for the Pro version but the event loading API is the same.

<?php
require_once '_db.php';

// .events.load() passes start and end as query string parameters by default
$start = $_GET["start"];
$end = $_GET["end"];
    
$stmt = $db->prepare('SELECT * FROM events WHERE NOT ((end <= :start) OR (start >= :end))');

$stmt->bindParam(':start', $start);
$stmt->bindParam(':end', $end);

$stmt->execute();
$result = $stmt->fetchAll();

class Event {}
$events = array();

foreach($result as $row) {
  $e = new Event();
  $e->id = $row['id'];
  $e->text = $row['name'];
  $e->start = $row['start'];
  $e->end = $row['end'];
  $e->backColor = $row['color'];
  $events[] = $e;
}

header('Content-Type: application/json');
echo json_encode($events);

Don't forget to enable errors (_db_mysql.php):

$db = new PDO("mysql:host=$host;port=$port",
    $username,
    $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

I also recommend checking the actual HTTP response using the development console in the browser (Network tab).

This question is more than 1 months old and has been closed. Please create a new question if you have anything to add.