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).