I asked another question related to RESTful services here, and have another question involving DELETE (PUT):
Same kind of situation where I want to send along user credentials (username/password) with my PUT. This is my DELETE service so far:
$http_req = $_SERVER["REQUEST_METHOD"];
switch ($http_req) {
case DELETE:
$user = $acct->read($_REQUEST["usernameDel"], $_REQUEST["passwordDel"]); // this method used to read db table with 1 record
if (!empty($_REQUEST["delTaskId"]) && isset($_REQUEST["usernameDel"]) && isset($_REQUEST["passwordDel"])) {
if ($user == true) {
$delete = fopen("php://input", "r");
$data = stream_get_contents($deleted);
$params;
parse_str($data, $params);
$dropped = $mgr->delete($params["id"]) // calls the method that deletes a particular row based on its id
echo $dropped;
}
}
}
This is the html page I am working with. I updated it to be in a :
<form action="TaskService.php" method="POST">
<label for="usernameDel">Username</label>
<input type="text" name="usernameDel" id="usernameDel" required/><br />
<label for="passwordDel">Password</label>
<input type="password" name="passwordDel" id="passwordDel" required/><br />
<label for="delTaskId">Task ID</label>
<input type="text" name="delTaskId" id="delTaskId" required/><br /><br />
<button type="button" id="btnDelete">Delete Task</button><br /><br />
</form>
This is my ajax code (edit-now using @Devs modifications):
$("#btnDelete").click(function() {
$.ajax({
method: "DELETE",
url: "TaskService.php",
data: { 'username':$("#usernameDel").val(), 'password': $("#passwordDel").val(), 'id': $("#delTaskId").val()},
success: function(theResponse) {
alert(theResponse);
}
});
});
Currently it returns a 200 response, but gives me the error messages I had put in for my Account class (comes back with "Could not find account".
My gut is telling me it has something to do with my $_REQUEST[]
superglobals since it's not doing anything with a form action, but I am not all too familiar with passing information via ajax
Thoughts?
Just arranged the ajax format, also some syntax error in the php code.
Note: never tested
Ajax
$(document).ready(function(){
$("#btnDelete").click(function() {
$.ajax({
method: "DELETE",
url: "TaskService.php",
data: { 'username':$("#usernameDel").val(), 'password': $("#passwordDel").val(), 'id': $("#delTaskId").val()},
success: function(theResponse) {
// Output
alert(theResponse); // comment this
}
});
});
});
TaskService.php
<?php
$http_req = $_SERVER["REQUEST_METHOD"];
switch ($http_req) {
case DELETE:
$user = $acct->read($_REQUEST["username"], $_REQUEST["password"]); // this method used to read db table with 1 record
if ($user == true) {
$delete = fopen("php://input", "r");
$data = stream_get_contents($deleted);
$params;
parse_str($data, $params);
$dropped = $mgr->delete($params["id"]); // calls the method that deletes a particular row based on its id
echo $dropped;
}
break;
}
?>
Found out why my validation wasn't working! I feel so blind!
I was ignoring the fact that the delete action was taking username, password, and id as parameters....as such, validation wasn't going to work because the "php://input" file hadn't been opened to parse through all 3 parameters.
Here's my updated PHP that works:
// I needed to access the file FIRST before doing anything with the parameters passed from AJAX
$deleted = fopen("php://input", "r");
$data = stream_get_contents($deleted);
$params;
parse_str($data, $params);
$user_auth = $acctMgr->readAcct($params["username"], $params["password"]);
if ($user_auth == true) {
$rows = $mgr->delete($params["id"]);
echo $rows;
} else {
echo "User not authenticated";
}