Skip to content

Instantly share code, notes, and snippets.

@amineeg
Created February 27, 2014 22:01
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save amineeg/9260523 to your computer and use it in GitHub Desktop.
Save amineeg/9260523 to your computer and use it in GitHub Desktop.
Get started angularJs, send data from angularJs to Php, decode json data in php, best practice
'use strict';
// Declare app level module which depends on filters, and services
var app= angular.module('myApp', ['ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'loginCtrl'});
$routeProvider.otherwise({redirectTo: '/login'});
}]);
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<title>My AngularJS App</title>
<link rel="stylesheet" href="css/app.css"/>
</head>
<body>
<div ng-view></div>
<!-- In production use:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
-->
<script src="lib/angular/angular.js"></script>
<script src="lib/angular/angular-route.js"></script>
<script src="js/app.js"></script>
<script src="js/directives/loginDrc.js"></script>
<script src="js/controllers/loginCtrl.js"></script>
<script src="js/services/loginService.js"></script>
</body>
</html>
'use strict';
app.controller('loginCtrl', ['$scope','loginService', function ($scope,loginService) {
$scope.msgtxt='';
$scope.login=function(data){
loginService.login(data,$scope); //call login service
};
}]);
'use strict';
app.directive('loginDirective',function(){
return{
templateUrl:'partials/tpl/login.tpl.html'
}
});
'use strict';
app.factory('loginService',function($http){
return{
login:function(data,scope){
var $promise=$http.post('data/user.php',data); //send data to user.php
$promise.then(function(msg){
if(msg.data=='succes') scope.msgtxt='Correct information';
else scope.msgtxt='incorrect information';
});
}
}
});
<?php
$user=json_decode(file_get_contents('php://input')); //get user from
if($user->mail=='elgaliamine@gmail.com' && $user->pass=='1234')
print 'succes';
else
print 'error';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment