Created
October 29, 2013 10:57
-
-
Save lifez/7212525 to your computer and use it in GitHub Desktop.
api/index.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2>Add new user</h2> | |
<form novalidate name="AddNewForm" id="add-new-form" method="post" action=""> | |
<label for="username">Username:</label> | |
<input type="text" ng-model="user.username" required /> | |
<label for="first_name">First name:</label> | |
<input type="text" ng-model="user.first_name" required /> | |
<label for="last_name">Last name:</label> | |
<input type="text" ng-model="user.last_name" required /> | |
<label for="address">Address:</label> | |
<input type="text" ng-model="user.address" /> | |
<br/> | |
<button class="btn btn-primary" ng-disabled="AddNewForm.$invalid || isUnchanged(user)" id="add-new-btn" ng-click="add_new(user)">Save!</button> | |
<a href="#/" class="btn">Cancel</a> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('CrudApp', []). | |
config(['$routeProvider', function($routeProvider) { | |
$routeProvider. | |
when('/', {templateUrl: 'assets/tpl/lists.html', controller: ListCtrl}). | |
when('/add-user', {templateUrl: 'assets/tpl/add-new.html', controller: AddCtrl}). | |
otherwise({redirectTo: '/'}); | |
}]); | |
function ListCtrl($scope, $http) { | |
$http.get('api/index.php/users').success(function(data) { | |
$scope.users = data; | |
}); | |
} | |
function TestCtrl($scope, $http) { | |
$scope.add_new = function(user, AddNewForm){ | |
$http.post('api/index.php/add_user', user).success(function(){ | |
console.log(user); | |
}); | |
}; | |
} | |
function AddCtrl($scope, $http, $location) { | |
$scope.master = {}; | |
$scope.activePath = null; | |
$scope.add_new = function(user, AddNewForm) { | |
console.log(user); | |
$http.post('api/index.php/add_user', user).success(function(){ | |
$scope.reset(); | |
$scope.activePath = $location.path('/'); | |
}); | |
$scope.reset = function() { | |
$scope.user = angular.copy($scope.master); | |
}; | |
$scope.reset(); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
require __DIR__ . '/../vendor/autoload.php'; | |
$app = new \Slim\Slim(); | |
$app->get('/users', 'getUsers'); | |
$app->post('/add_user', 'addUser'); | |
$app->post('/test','test'); | |
$app->run(); | |
function test(){ | |
$request = Slim::getInstance()->request(); | |
$user = json_decode($request->getBody()); | |
echo $data; | |
} | |
function getUsers() { | |
$sql = "select * FROM users ORDER BY id"; | |
try { | |
$db = getConnection(); | |
$stmt = $db->query($sql); | |
$wines = $stmt->fetchAll(PDO::FETCH_OBJ); | |
$db = null; | |
echo json_encode($wines); | |
} catch(PDOException $e) { | |
echo '{"error":{"text":'. $e->getMessage() .'}}'; | |
} | |
} | |
function addUser() { | |
$request = Slim::getInstance()->request(); | |
$sql = "INSERT INTO users (username, first_name, last_name, address) VALUES (:username, :first_name, :last_name, :address)"; | |
try { | |
$db = getConnection(); | |
$stmt = $db->prepare($sql); | |
$stmt->bindParam("username", $user->username); | |
$stmt->bindParam("first_name", $user->first_name); | |
$stmt->bindParam("last_name", $user->last_name); | |
$stmt->bindParam("address", $user->address); | |
$stmt->execute(); | |
$user->id = $db->lastInsertId(); | |
$db = null; | |
echo json_encode($user); | |
} catch(PDOException $e) { | |
echo '{"error":{"text":'. $e->getMessage() .'}}'; | |
} | |
} | |
function getConnection() { | |
$dbhost="localhost"; | |
$dbuser="root"; | |
$dbpass="root"; | |
$dbname="angular_crud"; | |
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); | |
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
return $dbh; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment