i unable add new item using slim php rest service , angular js, how add database?
below code: slim rest service code //function add individual location
function addlocation() { $app = \slim\slim::getinstance(); // $request = slim::getinstance()->request(); $location = json_decode($request->getbody()); $sql = "insert locations (location_title, location_latitude, location_longitude') values (:location_title, :location_latitude, :location_longitude)"; // try { $db = conn_db(); $stmt = $db->prepare($sql); $stmt->bindparam("location_title", $location->location_title); $stmt->bindparam("location_latitude", $location->location_latitude); $stmt->bindparam("location_longitude", $location->location_longitude); $stmt->execute(); $location->location_id = $db->lastinsertid(); $db = null; echo json_encode($location); } catch (pdeoxception $e) { echo 'error'; } } angular js code
//controller == add new page countryapp.controller('addnew', function($scope, $http, $location) { $scope.master = {}; $scope.activepath = null; $scope.add_new = function(location, addnewform) { $http.post('http://localhost/slimtest2/add_location', location).success(function() { $scope.reset(); $scope.activepath = $location.path('/'); }); $scope.reset = function() { $scope.location = angular.copy($scope.master); }; $scope.reset(); }; });
here example:
$app = new \slim\slim(); ... $app->post('/products', function() use ($app) { $data = json_decode($app->request->getbody()); $mandatory = array('name'); global $db; $rows = $db->insert("products", $data, $mandatory); if($rows["status"] == "success") $rows["message"] = "product added successfully."; echoresponse(200, $rows); }); function echoresponse($status_code, $response) { global $app; $app->status($status_code); $app->contenttype('application/json'); echo json_encode($response, json_numeric_check); } $app->run(); angular code call fuction
$scope.saveproduct = function (product) { ... products.post('products', product).then(function (result) { if (result.status != 'error') { console.log(result); var x = angular.copy(product); x.save = 'insert'; x.id = result.data; $modalinstance.close(x); } else { console.log(result); } }); ... and template
<form name="product_form" class="form-horizontal" role="form" novalidate> <form-element label="name" mod="product"> <input type="text" class="form-control" name="name" placeholder="name" ng-model="product.name" ng-disabled="product.id" focus/> </form-element> <form-element label="description" mod="product"> <textarea class="form-control" name="description" placeholder="description" ng-model="product.description">{{product.description}}</textarea> </form-element> ... <button ng-click="saveproduct(product);" ng-disabled="product_form.$invalid || enableupdate" class="btn btn-sm btn-primary" type="submit"> <i class="ace-icon fa fa-check"></i>{{buttontext}} </button> you have example angular part: building basic crud application using angularjs , slim php framework (part 1) , great example slim part restful services jquery, php , slim framework
i hope, helped you.
Comments
Post a Comment