2

This is how I want to delete a record using jquery ajax

deleteFile: function(obj) {
        $.ajax({
            type: 'delete',
            dataType: 'json',
            url: 'service/lead.php?a=deleteFile',
            data: {id: $(obj).attr('data-lf-id')}
        }).done(function(response) {
            console.log("done");
        }).fail(function(error) {

        });
    }

This works but how am I suppsed to get the id value on the lead.php page? This is what I am currently doing but it doesn't capture the id value.

//service/lead.php
if ($_SERVER['REQUEST_METHOD'] == "DELETE") {
    if ($_GET['a'] == 'deleteFile') {
        echo json_encode($lead->deleteLeadFile($_REQUEST['id']));
    }
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
u54r
  • 1,767
  • 2
  • 15
  • 26
  • What does `print_r($_GET)` show? – John Conde Sep 25 '13 at 20:13
  • It only shows this `[a] => deleteFile` – u54r Sep 25 '13 at 20:14
  • what does `$(obj).attr('data-lf-id')` show? Also, what does `print_r($_REQUEST)` show? – Joseph Callaars Sep 25 '13 at 20:15
  • What does print_r($_REQUEST) show? – John Conde Sep 25 '13 at 20:16
  • Thats is the id I want to match in the database and then delete that record. I see in that value is sent to the `lead.php` page. – u54r Sep 25 '13 at 20:16
  • Since it isn't a GET request, it should be getting sent as a param string to the request body. Open your debugging console and inspect the request headers being sent. – Kevin B Sep 25 '13 at 20:17
  • @KevinB, yes I see it in the devtool form data `id` is sent to the `lead.php` – u54r Sep 25 '13 at 20:18
  • form data... did you try looking at $_POST? I wouldn't have expected it to be there, but if it's sending it as form data, might as well try. – Kevin B Sep 25 '13 at 20:18
  • `$_POST` is empty and `$_REQUEST` is same as `$_GET` which only shows `[a] => deleteFile` – u54r Sep 25 '13 at 20:20
  • possible duplicate of [How to pass data in the ajax DELETE request other than headers](http://stackoverflow.com/questions/15088955/how-to-pass-data-in-the-ajax-delete-request-other-than-headers) – Joseph Callaars Sep 25 '13 at 20:21
  • http://stackoverflow.com/questions/6535340/is-it-possible-to-append-request-parameters-to-a-http-delete-request-in-ajax – tekstop Sep 25 '13 at 20:36
  • A `DELETE` HTTP request is telling your web server you want to delete the file `service/lead.php`. It has nothing to do with what you are planning. – Havenard Sep 25 '13 at 20:57

2 Answers2

1

I would suggest changing your Ajax type to either 'POST' or 'GET". You can then use the appropriate $_GET or $_POST variable to retrieve your id.

deleteFile: function(obj) {
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: 'service/lead.php?a=deleteFile',
            data: {id: $(obj).attr('data-lf-id')}
        }).done(function(response) {
            console.log("done");
        }).fail(function(error) {

        });
    }

//service/lead.php

if ($_GET['a'] == 'deleteFile') {
    echo json_encode($lead->deleteLeadFile($_POST['id']));
}
Gary
  • 2,866
  • 1
  • 17
  • 20
1
file_get_contents(“php://input”)

This always gives you raw request data - you have to parse it [with parse_str if you send urlencoded data, with json_decode if you send JSON].

But you should keep it RESTful and not RPC-like, and issue a delete over /resource/:id.

moonwave99
  • 21,957
  • 3
  • 43
  • 64