0

I am trying to implement delete functionality in angularjs using php as follows: delete: index.html view:

<ul>
            <li ng-repeat="r in result">
            {{r.description}}|{{r.name}} | <a href='' ng-click = "edit(r.product_id)">edit</a> | <a href='' ng-click = "delete(r.product_id)">delete</a>
            <input type="hidden" name="hdnid" ng-model="hdn" value="{{r.product_id}}"/>
            </li>
        </ul>

in controller: delete:

//delete
        $scope.delete = function() {
            var elem = angular.element($element);
            var dt = $(elem).serialize();
            //alert($element);
            console.log($(elem).serialize());
            $http({
                method: 'POST',
                url: 'php/delete.php',
                data: dt,
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).success(function(data, status) {
                $scope.status = status;
                $scope.data = data;
                $scope.rs = data; // Show result from server in our <pre></pre> element
            }).error(function(data, status) {
                $scope.data = data || "Request failed";
                $scope.status = status;
            });
        };

and in php file: delete.php

    <?php
include 'connect.php';
mysql_select_db($database,$con);  
$id = $_POST['hdnid'];
$query = "delete from `product` where `product_id` = $id";
$result = mysql_query($query) OR die(mysql_error()); 
echo "You have successfully deleted ";
?>

The record gets deleted but the page is not refreshed. I cant see the record deleted until I refresh the page. Where am I going wrong? how do I refresh the page?

3
  • I'm confused: why do you assign the result of AJAX operation to $scope.rs and $scope.data, but do not change $scope.result in any way? Commented Aug 31, 2012 at 13:10
  • i am a complete noob to angularjs so i dodnt know about this. How should i change $scope.result? and what actually is result? Commented Sep 1, 2012 at 8:13
  • tech-blog.maddyzone.com/javascript/… hope it will helpfull Commented Feb 10, 2014 at 16:16

1 Answer 1

2

You need to delete it from your scope for it to show up. We don't need to pass the element Id to the scope to get it to work when we click we can capture it with the this keyword

 $scope.delete = function() {
        //Store the this variable so we can use it in the $http functions
        var that = this
        var elem = angular.element($element);
        var dt = $(elem).serialize();
        //alert($element);
        console.log($(elem).serialize());
        $http({
            method: 'POST',
            url: 'php/delete.php',
            data: dt,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function(data, status) {
            // get the elements index and then remove it from the results array

            $scope.result.splice(that.$index, 1);
            $scope.status = status;
            $scope.data = data;
            $scope.rs = data; // Show result from server in our <pre></pre> element
        }).error(function(data, status) {
            $scope.data = data || "Request failed";
            $scope.status = status;
        });
    };
Sign up to request clarification or add additional context in comments.

3 Comments

i tried it but still the same. btw what is $index? is it provided by angularjs? i tried it as $scope.products.splice(that.$index, 1); and then $scope.products = data;
when you use ng-repeat it makes an array index for every item it looped for accessible via the items $index property.
Take a loook at this fiddle I made see if you can use anything there. jsfiddle.net/EJ8fW

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.