0

I want delete when click in button , but my code not work . Here is my code html

<tr class="row" ng-repeat="data in vm.mydata >
                <td class="col-md-1"> <span>{{data.masp}}</span></td>
                <td class="col-md-1"> <span>{{data.hangsx}}</span></td>
        </tr>
    <i class="fa fa-trash-o fa-2x" aria-hidden="true" ng-click="vm.delete(data.masp)"></i>

angular

function dsController($http){
        var vm =this;
$http.get('/project/app/server/data/data.php')
             .then(onSuccess);

        function onSuccess(response){
            vm.mydata = response.data.records;
        } 
vm.delete = function(value){
            $http({
                method:'POST',
                url:'/project/app/server/action/xoa.php',
                data:({value:value}),
                headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).then(onSuccess,onError);
            function onSuccess(){
            $http.get('/project/app/server/data/data.php')
                .then(function(data){
                    vm.mydata = data.data.records;
                })
}

and here is php code : xoa.php

    //connect db
    if(!empty($_POST['value'])){
    $masp = $_POST['value'];
    $sql = "DELETE FROM sanpham WHERE masp= '$masp'";
    $result = mysqli_query($conn,$sql);
   if($result){
  echo 'OK';

} data.php

$rows = array();
if($result){
while($r = mysqli_fetch_assoc($result)) {
    $rows[] = $r;
}
print '{"records":' .json_encode($rows) ."}";

Where is my wrong . Pls help me

10
  • Why are you making an HTTP POST request for deleting an object? You should use HTTP DELETE. Commented Mar 27, 2017 at 16:29
  • is data getting removed from the table Commented Mar 27, 2017 at 16:33
  • Your above code seems fine. Can you please share the error if you are getting any? Or please confirm from db table that record deleted or not? Commented Mar 27, 2017 at 16:34
  • I think you forgot to add this line which you added just now? $result = mysqli_query($conn,$sql); Commented Mar 27, 2017 at 16:36
  • I change to delete but not work to . and data not remove from the table , and no error Commented Mar 27, 2017 at 16:36

2 Answers 2

1

In html you add

vm.delete(data.masp,$index)

in js you change

vm.delete = function(value,index)
{
method:'GET',
url:'/project/app/server/data/data.php?value=' +value
}
Sign up to request clarification or add additional context in comments.

Comments

0

try like this :

                               vm.delete  = function (value) {
                                    var Response = $http({
                                        method:'POST',
                                            url:'/project/app/server/action/xoa.php',
                                            data:{value: JSON.stringify(value)},
                                    });
                                    return Response;
                                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.