標籤:oct back sel fetch div roo getattr 第一個 query
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div ng-app="myApp" ng-controller="myctrl">
<table width=‘100%‘ border="1" cellspacing="0">
<tr>
<th>姓名</th>
<th>性別</th>
<th>年齡</th>
<th>操作</th>
</tr>
<tr ng-repeat="x in list">
<td ng-bind=" x.username"></td>
<td ng-bind=" x.userage"></td>
<td ng-bind=" x.usersex"></td>
<td><input type="button" value="刪除" uid={{x.userid}} ng-click="del($event)"><input type="button" value="修改" uid={{x.userid}}></td> //uid是自己定的屬性 並賦值 del(傳自己為對象進去) 相當於js 的this
</tr>
</table>
</div>
</body>
<script src="js/angular.min.js" charset="utf-8"></script>
<script type="text/javascript">
var app=angular.module(‘myApp‘,[]);
app.controller(‘myctrl‘,function($scope,$http){
$http({
method:‘GET‘,
url:"php/angu_test_sel.php"
}).then(function successCallback(response){
$scope.list=response.data.msg;
},function errorCallback(response){
alert("資料請求失敗");
});
$scope.del=function(event){
var id=event.target.getAttribute("uid");//擷取當前對象的uid值
$http({
method:‘GET‘,
params :{ id : id},//第一個id是後台php接受的參數名稱 第二個id是本頁面擷取的刪除的id值
url:"php/angu_test_del.php"
}).then(function successCallback(response){
window.location.reload();
},function errorCallback(response){
alert("刪除失敗");
});
}
})
</script>
<?php
header(‘Content-Type: text/html; charset=utf8‘);
$conn=mysql_connect("localhost","root","root");
mysql_query("use txt");
if($conn){
if($_GET){
$uid=$_GET["id"];
$sql="delete from test1 where userid=$uid";
$res=mysql_query($sql);
if($res>0){
echo ‘{"status":"1"}‘;
}else{
echo ‘{"status":"0"}‘;
}
}
}else{
echo ‘{"status":"-2"}‘;
}
?>
<?php
header(‘Content-Type: text/html; charset=utf8‘);
$conn=mysql_connect("localhost","root","root");
mysql_query("use txt");
if($conn){
$sql="select * from test1";
$res=mysql_query($sql);
if(mysql_num_rows($res)>0){
while($row=mysql_fetch_assoc($res)){
$arr[]= $row;
}
echo ‘{"status":"1","msg":‘.json_encode($arr).‘}‘;
}
}else{
echo ‘{"status":"-2";"msg":"fail to connect"}‘;
}
?>
angularjs串連資料庫尋找刪除操作