php - How to POST from Angular to Slim? -
i have following code, doesn't seem work. tried search solution, of them older , specific answers.
i point:
- the template loads in ng-view via config.
- the form submits on click.
- nothing happens.
i check post google console in network, , doesn't fire.
what missing? got of code this article.
html view i.e. form
<h2>add new job</h2> <form novalidate name="addnewform" method="post" action=""> <input type="text" class="form-control" ng-model="job.title" placeholder="title" required /> <input type="text" class="form-control" ng-model="job.company" placeholder="company" required /> <input type="text" class="form-control" ng-model="job.description" placeholder="description" required /> <input type="text" class="form-control" ng-model="job.location" placeholder="location" required /> <br/> <button ng-disabled="addnewform.$invalid || isunchanged(job)" ng-click="add_new(job)" class="btn">save!</button> </form>
angular config controller
(function() { angular .module('app') .config(function($routeprovider) { $routeprovider.when('/add-job', { templateurl: 'templates/add-job.html', controller: addcontroller }); }); function addcontroller($http, $location) { var = this; that.master = {}; that.activepath = null; that.add_new = function(job, addnewform) { $http.post('api/add_job', job).success(function () { alert("added!"); that.reset(); that.activepath = $location.path('/'); }); that.reset = function () { that.job = angular.copy(that.master); }; that.reset(); }; } })();
slim api
require 'vendor/autoload.php'; $app = new \slim\app; $app->post('/add_job', 'addjob'); $app->run(); function addjob() { $request = slim::getinstance()->request(); $job = json_decode($request->getbody()); $sql = "insert jobs (title, company, description, location) values (:title, :company, :description, :location)"; try { $db = getconnection(); $stmt = $db->prepare($sql); $stmt->bindparam("title", $job->title); $stmt->bindparam("company", $job->company); $stmt->bindparam("description", $job->description); $stmt->bindparam("location", $job->location); $stmt->execute(); $job->id = $db->lastinsertid(); $db = null; echo json_encode($job); } catch(pdoexception $e) { echo '{"error":{"text":'. $e->getmessage() .'}}'; } }
you using controlleras
syntax in controller $scope
syntax in view. note article link uses $scope
in controller variables match current view setup
you need pick 1 or other view compatible controller
Comments
Post a Comment