删除angularjs中的数据库数据

时间:2016-08-29 06:43:13

标签: javascript php jquery angularjs angularjs-scope

当我将此链接放在浏览器上时,它会起作用。但是,当点击按钮时它将无法正常工作。这段代码有什么问题。 http://localhost/youtubewebservice/shopCartProductDelete.php?cart_ID=6

$scope.delete = function(cart_ID, index) {
var params = $.param({"cart_ID":cart_ID});

  console.log(cart_ID);
  $http({
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    url: 'http://localhost/youtubewebservice/shopCartProductDelete.php?cart_ID=$cart_ID',
    method: "GET",
    data: params
  }).success(function(data){
    $scope.data.splice(index, 1);
  });
} 
<img src="img/removecart.png" ng-click="delete({{produ.cart_ID}}, $index)" style="max-height: 40px;margin-right: 15px;"/>

PHP代码

<?php
$con = mysqli_connect("localhost","root","","look4com_lk");
if(isset($_GET['cart_ID'])){
$cart_ID = $_GET['cart_ID'];
$res = "DELETE FROM l4wlk_cart WHERE cart_ID='".$cart_ID."'";
mysqli_query($con, $res);


}

echo json_encode($result);

?>

3 个答案:

答案 0 :(得分:0)

在angular指令中,您不需要解析角度值。你应该改变这个

<img src="img/removecart.png" ng-click="delete({{produ.cart_ID}}, $index)" style="max-height: 40px;margiurln-right: 15px;"/>

<img src="img/removecart.png" ng-click="delete(produ.cart_ID, $index)" style="max-height: 40px;margin-right: 15px;"/>

<强>更新

尝试更改您的js代码,如下所示

$http.get('http://localhost/youtubewebservice/shopCartProductDelete.php', {"cart_ID":cart_ID})
.success(function(data){
   $scope.data.splice(index, 1);
});

只需查看

即可
if(isset($_GET['cart_ID'])){
  $cart_ID = $_GET['cart_ID'];
  $res = "DELETE FROM l4wlk_cart WHERE cart_ID='".$cart_ID."'";
  mysqli_query($con, $res) or mysqli_error($con);
}else{
  die("Param value not set up");
}

答案 1 :(得分:0)

请参阅ng-click

the documentation

enter image description here

它使用expression

如果在template列中写了Type,那么您可以用双花括号{{..}}编写这些值。但由于它接受一个表达式,因此不需要使用双花括号。

将您的<img..>更改为:

<img src="img/removecart.png" ng-click="delete(produ.cart_ID, $index)" style="max-height: 40px;margin-right: 15px;"/>

答案 2 :(得分:-1)

而不是console.log(cart_ID),请检查console.log(produ.cart_ID)

的值