Q:

How do you access data in php when the request method sent from backbone is delete?

I am using backbone.js for the first time and I am using in conjunction with php and mysql so that I can send and receive data from a database using backbone and I am having a problem with  the delete request method. I cannot access the data that is sent along with the request method like I do with post and put. POST and PUT work perfectly in this script it is just delete I am having problems with.
Here is my code.
helloWorld.js
(function($) {

var Item = Backbone.Model.extend({
    url: 'http://mysite.com/syncItem.php', 
    defaults: {
        part1: 'hello',
        part2: 'world',
    }
});

var List = Backbone.Collection.extend({
    model: Item 
});

var ItemView = Backbone.View.extend({
    tagName: 'li', 
    events: {
        'click span.swap': 'swap', 
        'click span.delete': 'remove'
    },

    initialize: function() {
        _.bindAll(this, 'render', 'unrender', 'swap', 'remove'); 

        this.model.bind('change', this.render); 
        this.model.bind('remove', this.unrender);
    },

    render: function() {
        $(this.el).html('<span style="color:black;">' + this.model.get('part1') +' '+this.model.get('part2')+'</span> &nbsp; &nbsp; <span class="swap" style="font-family:sans-serif; color:blue; cursor:pointer;">[swap parts]</span> <span class="delete" style="cursor:pointer; color:red; font-family:sans-serif;">[delete]</span>');
        return this; 
    },

    unrender: function() {
        $(this.el).remove();
    },

    swap: function() {
        var swapped = {
            part1: this.model.get('part2'),
            part2: this.model.get('part1')  
        };
        this.model.set(swapped);
        this.model.sync("update", this.model);
    },

    remove: function() {
        this.model.sync("delete", this.model);  //deletes Item from server DB
        this.model.destroy(); //deletes Item from collection
    }
});

var ListView = Backbone.View.extend({
    el: $('div#backboneContainer'),  

    events: {
        'click button#add': 'addItem'   
    },

    initialize: function() {
        _.bindAll(this, 'render', 'addItem', 'appendItem');

        this.collection = new List(); 
        this.collection.bind('add', this.appendItem); 

        this.counter = 0;
        this.render();  
    },

    render: function() {
        var self = this;
        $(this.el).append("<button id='add'>Add List Item</button>");
        $(this.el).append("<ul></ul>");
    },

    addItem : function() {
        this.counter++;
        item = new Item({id: this.counter}); 
        item.set({
            part2: item.get('part2') + this.counter 
        });
        this.collection.add(item); 
        item.sync("create", item);
    },

    appendItem: function() {
        var itemView = new ItemView({
            model: item 
        });
        $('ul', this.el).append(itemView.render().el);
    }
});

var listView = new ListView();
})(jQuery);

syncItem.php
<?php
include("includes/openDbConn.php");
$request_method = strtolower($_SERVER['REQUEST_METHOD']);
switch($request_method) {
    case 'post': {
        $data = json_decode(file_get_contents('php://input'));
        if($stmt = $mysqli->prepare("INSERT INTO backbonetest (modelId, part1, part2) VALUES (?, ?, ?)"))
        {
            $stmt->bind_param("iss", $data->{'id'}, $data->{'part1'}, $data->{'part2'});
            $stmt->execute();
        }
        break;  
    }
    case 'get': {
        //NOT FINISHED
        break;  
    }
    case 'put': {
        $data = json_decode(file_get_contents('php://input')); 
        if($stmt = $mysqli->prepare("UPDATE backbonetest SET part1 = ?, part2 = ? WHERE modelId = ?"))
        {
            $stmt->bind_param("ssi", $data->{'part1'}, $data->{'part2'}, $data->{'id'});
            $stmt->execute();
        }
        break;
    }
    case 'delete': {
        $data = json_decode(file_get_contents('php://input')); //gets data from the json that was sent with the request
        if($stmt = $mysqli->prepare("DELETE FROM backbonetest WHERE modelId = ?"))
        {
            $stmt->bind_param("i", $data->{'id'});
            $stmt->execute();
        } 
        break;  
    }
}
?>

I have narrowed down where the error is to two spots: where I call this.model.sync("delete", this.model); or $data = json_decode(file_get_contents('php://input')); in the delete case. So I was wondering if there was a special way to access the data when dealing with the delete request method or am I just calling the delete function in backbone wrong?

A:

You have a problem of unfulfilled expectations :) DELETE requests in Backbone don't send by default the attributes as POST, PUT and PATCH do1 : compare the requests sent by model.save and model.destroy in this Fiddle http://jsfiddle.net/fz68a/
You can override Item.sync to give it the behavior you expect:
var Item = Backbone.Model.extend({
    defaults: {
        part1: 'hello',
        part2: 'world'
    },

    sync: function (method, model, options) {
        if (method === 'delete') {
            options = options || {};
            options.contentType = 'application/json';
            options.data = JSON.stringify(this.toJSON());
        }

        return Backbone.sync.call(this, method, model, options);
    }
});

And a demo http://jsfiddle.net/fz68a/1/

1 See Backbone source code http://documentcloud.github.io/backbone/docs/backbone.html#section-139

