[英]i am not able to update the record using update function into the database using php and angularjs
這是我的表格代碼:
<tbody>
<tr ng-repeat="tabdata in tabledata">
<td>
<div ng-hide="editdata[tabdata.id]">{{tabdata.name}}</div>
<div ng-show="editdata[tabdata.id]"><input type="text" ng-model="tabdata.name"></div>
</td>
<td>
<div ng-hide="editdata[tabdata.id]">{{tabdata.city}}</div>
<div ng-show="editdata[tabdata.id]"><input type="text" ng-model="tabdata.city"></div>
</td>
<td>
<button ng-hide="editdata[tabdata.id]" ng-click="edit(tabdata)">EDIT</button>
<button ng-show="editdata[tabdata.id]" ng-click="update(tabdata)">UPDATE</button>
</td>
</tr>
</tbody>
這是我的代碼:
var app=angular.module('myApp',[]);
app.controller('myCtrl',['$scope','$http',function($scope,$http){
在這里,我從數據庫表中獲取數據:
$http.get('fetch.php').success(function(data) {
$scope.tabledata = data;
});
$scope.editdata={};
此功能用於編輯按鈕:
$scope.edit=function(tabdata){
$scope.editdata[tabdata.id]=true;
};
這是用於更新按鈕:
$scope.update=function(tabdata) {
$scope.editdata[tabdata.id] = false;
在這里,我必須調用ajax方法將值更新到數據庫中。 或者我們也可以使用服務功能?
我在我的fetch.php文件中使用$ input使用orm文件來連接數據庫。
$http({
method:"post",
url:'fatch.php',
data:{
}
})
};
}]);
問題是當我點擊編輯按鈕時它會被修改但在那之后,當我編輯一些行數據時,它只更新靜態頁面中的數據。 我也必須更新我的數據庫表。 那怎么辦呢?
我必須使用SERVICE函數和AJAX調用方法更新我的數據庫表。 知道怎么做呢?
這是您的案例的解決方案:
$scope.update=function(tabdata) {
$scope.editdata[tabdata.id] = false;
$http({
method:"post",
url:'fetch.php',
data:{
// your data to send
}
}).then(function(success){
// refresh live your tabledata angularjs will do the job for you
$http.get('fetch.php').success(function(data) {
$scope.tabledata = data;
});
})
}
在您的情況下,您不需要Ajax調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.