2

I'm working on making a small application just to get the hang of REST and I'm having some trouble with the DELETE method. I've never used it before so I'm not sure how it behaves.

Anyway, I followed this tutorial to get a hang of the basics. I modified it so instead of using a pre-made array, the user can submit data and view it from a MySQL databse.

I have 3 files:

  • server.php - the "API" which determines the method used and acts accordingly

  • input.php - displays a form for the user to input data

  • viewinput.php - displays the inputs that have been entered.

I'm trying to now place a "delete" button on viewinput.php so that an entry can be deleted. Here's my code that displays the entered information.

while ($result = mysql_fetch_array($sql)){
        ?>
        <tr><td><? echo $result['id']." "; ?></td><td><? echo $result['text']; ?></td>
        <form method = "delete" >
            <td><input type="submit" name="delete" value="delete"></input></td></tr>
            <input type="hidden" name = "hidden_delete" value="<? echo $result['id']; ?>"></input>
        </form>
        <?
    }

Now, in my server.php file (the API), this is the very first function that is called which determines the method and breaks the URL into components for further processing.

public function serve() {
    $uri = $_SERVER['REQUEST_URI'];
    echo $method = $_SERVER['REQUEST_METHOD'];   //GET and POST are displayed, DELETE isn't
    $paths = explode('/', $this->paths($uri));
    array_shift($paths); // 
    $resource = array_shift($paths);

When I press a delete button, the URL goes from

/rest/viewinput

to

/rest/viewinput?delete=delete&hiddendelete=3 //assuming I deleted the 3rd entry

From what I understand, the URL should be /rest/viewinput/3 when a DELETE method is submitted

In my server.php file, when I echo the method, "DELETE" isn't shown like it does for POST and GET methods.

I found this resource regarding DELETE, and from what I understand an identifier will be passed through the URL, but there should be some method received just like GET and POST (meaning my code should display DELETE when I echo the method).

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1104854
  • 2,137
  • 12
  • 51
  • 74

1 Answers1

4

Here is the current lowdown on PUT and DELETE.

If you are "plain browsing", most browsers don't actually use either of these verbs. They do however support them for XML Http Requests (i.e. AJAX). So from the client side, you can't really use PUT or DELETE directly on an HTML form element and have it work. You would need to use the work-around suggested by Complex857, i.e. include the intention in the request.

If you are using background AJAX calls, or indeed if you are not using a browser to make the requests, you can use the full suite of HTTP verbs, including PUT and DELETE. One caveat to this is that on IIS you may need to change your settings to allow these verbs as you can set the specific verbs your server accepts.

Fenton
  • 241,084
  • 71
  • 387
  • 401