I am unable to add a new item using a Slim PHP Rest Service and Angular JS, how do I get it to add to the database?
Below is my code: Slim REST Service Code //function to add an individual location
function AddLocation()
{
$app = \Slim\Slim::getInstance();
//
$request = Slim::getInstance()->request();
$location = json_decode($request->getBody());
$sql = "INSERT INTO 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 is an 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 the 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 good example for angular part: Building a Basic CRUD Application Using AngularJS and Slim PHP framework (Part 1) and great example for the slim part RESTful services with jQuery, PHP and the Slim Framework
I hope, I helped you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.