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

How do I delete the event from the database?

Asked by Anonymous
8 years ago.

Okay I've been trying to do that but I don't know how.
dp.onEventDelete = function(args) {
if (!confirm("Do you really want to delete this event?")) {
args.preventDefault();
}
};

Uhm.. I guess it's because or args.preventDefault()? But I want to permanently delete the event in the database

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

You need to call the server using an AJAX call, like this:

<div id="dp"></div>
<script type="text/javascript">
  var dp = new DayPilot.Calendar("dp");
  dp.eventDeleteHandling = "Update";

  dp.onEventDelete = function(args) {
    if (!confirm("Do you really want to delete this event?")) {
      args.preventDefault();
    }
  };

  dp.onEventDeleted = function(args) {

    // AJAX call to the server, this example uses jQuery
    $.post("/event/delete/" + args.e.id(), function(result) {
      dp.message("Event deleted: " + args.e.text());
    });

  };
  // ...
  dp.init();
</script>

See also:
http://doc.daypilot.org/calendar/event-deleting/

Comment posted by Anonymous
8 years ago.

When I refresh the page it was not deleted..

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

There is no magic in there - you need to implement the endpoint called by the onEventDeleted event handler ("/event/delete/id").

In PHP it could look like this:

HTML

<div id="dp"></div>
<script type="text/javascript">
  var dp = new DayPilot.Calendar("dp");
  dp.eventDeleteHandling = "Update";

  dp.onEventDelete = function(args) {
    if (!confirm("Do you really want to delete this event?")) {
      args.preventDefault();
    }
  };

  dp.onEventDeleted = function(args) {
    // AJAX call to the server, this example uses jQuery
    $.post(
      "backend_delete.php", 
      { id: args.e.id()}, 
      function(result) {
        dp.message(result.message);   // "Delete successful" received from the server
      }
    );
  };
  // ...
  dp.init();
</script>

backend_delete.php

<?php
require_once '_db.php';

$stmt = $db->prepare("DELETE FROM events WHERE id = :id");
$stmt->bindParam(':id', $_POST['id']);
$stmt->execute();

class Result {}

$response = new Result();
$response->result = 'OK';
$response->message = 'Delete successful';

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

?>

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