How to delete data using angular, php and ui-route?

Asked

Viewed 332 times

1

I am trying to make a method to delete a record from the bank and the following message seems on the console:

GET http://localhost:8888/systems/systems_web/Vigilantescomunitarios/admin/php/apagaPais.php? idPais=Undefined 500 (Internal Server Error)

I’m using angular ui-router.

Follow my codes:

html:

<table width="200">
    <tr>
        <td><b>País</b></td>
        <td><b>Sala</b></td>
    </tr>
    <tr ng-repeat="pais in paises">
        <td>{{pais.nome}}</td>
        <td>{{pais.sala}}</td>
        <td><a ui-sref="editarPais({idPais: pais.idPais})">[ ]</a></td>
        <td><a ng-click="apagarPais(pais.idPais)">[x]</a></td>
    </tr>
</table>

Angular:

app.controller("PaisesController", function ($scope, $http, $stateParams, $state) {

$scope.apagarPais = function (pais) {
    console.log("id "+pais);
    $http.get("admin/php/apagaPais.php?idPais="+pais)
    .success(function (data, status){
        console.log(data);
        carregaPaises();
    });
};

carregaPaises();

});

php:

include_once("conexao.php");
$pdo = conectar();

$id = $_POST['idPais'];
print_r($id);

$apagaPais=$pdo->prepare("DELETE FROM paises WHERE idPais=:id");
$apagaPais->bindValue(":id", $id);
$apagaPais->execute();

1 answer

2

You need to inform PHP which ID should be removed, for that you need to SEND the angular ID to PHP.

Use $http.post instead of $http.get.

Another observation, the method .then() will only work if you have a promise being returned, which is not the case of the method .get. The error may be so, but still the deletion will not work.

  • I made the changes and the message continues. "POST http://localhost:8888/systems/systems_web/Watchersmunitarios/admin/php/apagaPais.php? idPais=Undefined 500 (Internal Server Error)" .

  • As I have already answered in other questions of yours, use the print and console methods. This error by itself says it all, there was some internal error of the server, not having connection with the angular. The $id is really getting the correct value?

  • Had already placed console and print_r. The console, shows the id being passed. Already print_r does not return anything. I put in the code, here of the post, the consoles and prints I put in my code to see what returns. Only the first console that shows the id, the print does not appear.

  • I was able to pass the id as parameter, see how I did the code update, here in the post. .

  • I got it. I was doing include with wrong connection file.

  • 1

    Yes, I was finding strange the server error, usually if it is error in the angular, it comes with a link to the site of the angular itself, where it contains a description of what may be happening. But if you can solve it, great! = D

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.