Maihan Nijat
Maihan Nijat

Reputation: 9344

How to delete record from table using Angular http.delete with PHP backend?

I am trying to delete a record from the database table using Angular on frontend and PHP on the backend. I am passing the id through URL and trying to get it back with $_GET['id'] but it doesn't work (no error in console).

PHP API:

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: DELETE");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

require_once("includes/initialize.php");

$db = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbdatabase) or die('Could not connect to database');

$data = json_decode(file_get_contents("php://input"));

$id;

if (isset($_GET['id'])) {
    $id = $_GET['id'];
}


// update the ECD
if(ECD::deleteECD($id)){
    echo '{';
    echo '"message": "The center was deleted."';
    echo '}';
}

// if unable to update the ECD, tell the user
else{
    echo '{';
    echo '"message": "Unable to delete the center."';
    echo '}';
}

And Angular http.delete:

deleteCenter (id: number): Observable<{}> {
  const url = `${this.D_ROOT_URL}${id}`;
  return this.http.delete(url);
}

I tested the URL to make sure it's passing the correct URL.

Upvotes: 2

Views: 698

Answers (1)

erosb
erosb

Reputation: 3141

The delete() method returns a cold observable, which means that the HTTP request is not sent until someone subscribes to the observable. You should write this:

return this.http.delete(url).subscribe();

Upvotes: 2

Related Questions