PHP RESTful services; AJAX and DELETE method with username/password credentials -
i asked question related restful services here, , have question involving delete (put):
same kind of situation want send along user credentials (username/password) put. delete service far:
$http_req = $_server["request_method"]; switch ($http_req) { case delete: $user = $acct->read($_request["usernamedel"], $_request["passworddel"]); // method used read db table 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 method deletes particular row based on id echo $dropped; } } }
this html page working with. updated in :
<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 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 returns 200 response, gives me error messages had put in account class (comes "could not find account".
gut telling me has $_request[]
superglobals since it's not doing form action, not familiar passing information via ajax
thoughts?
just arranged ajax format, syntax error in 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 } }); }); });
taskservice.php
<?php $http_req = $_server["request_method"]; switch ($http_req) { case delete: $user = $acct->read($_request["username"], $_request["password"]); // method used read db table 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 method deletes particular row based on id echo $dropped; } break; } ?>
Comments
Post a Comment