Last active
December 24, 2015 00:09
-
-
Save whisher/6714864 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| //php | |
| <?php | |
| $data = file_get_contents("php://input"); | |
| $data = json_decode($data,true); | |
| if(isset($data['email'])){ | |
| if(empty($data['email'])){ | |
| header("HTTP/1.0 404 Not Found"); | |
| exit; | |
| } | |
| elseif($data['email'] !== '[email protected]'){ | |
| header("HTTP/1.0 404 Not Found"); | |
| exit; | |
| } | |
| else{ | |
| header("HTTP/1.1 200 OK"); | |
| echo json_encode(array('data'=>array('email'=>$data['email']))); | |
| exit; | |
| } | |
| } | |
| //html | |
| <!doctype html> | |
| <html data-ng-app="myApp"> | |
| <head> | |
| <meta charset="utf-8"> | |
| </head> | |
| <body> | |
| <div data-ng-controller="myCtrl"> | |
| <form novalidate id="frm-signup" name="addContestantFrm" data-ng-submit="add()"> | |
| <div> | |
| <label for="email">Email: *</label> | |
| <input type="email" id="email" name="email" class="input-medium" tabindex="3" title="email" maxlength="255" value="{{contestant.email}}" placeholder="email" data-ng-model="contestant.email" required email-unique /> | |
| </div> | |
| <div> | |
| <input type="submit" id="sbmt" name="sbmt" class="input-sbt" data-ng-disabled="!addContestantFrm.$valid" value="Send" /> | |
| </div> | |
| </form> | |
| </div> | |
| <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> | |
| <script src="http://code.angularjs.org/1.0.8/angular.min.js"></script> | |
| <script> | |
| var app = angular.module('myApp', []); | |
| app.factory('Contestant',function($http){ | |
| return { | |
| checkUniqueEmail : function(email){ | |
| var promise = $http.post('./checkemail.php',{email:email}).then(function (response) { | |
| console.log(response); | |
| // The return value gets picked up by the then in the controller. | |
| return response.data.data.email; | |
| }); | |
| return promise; | |
| } | |
| } | |
| }); | |
| app.controller('myCtrl',function($scope){ | |
| $scope.add = function(){ | |
| console.log($scope.contestant); | |
| } | |
| }); | |
| app.directive('emailUnique',function(Contestant) { | |
| return { | |
| require: 'ngModel', | |
| link: function(scope, element, attrs,ctrl) { | |
| ctrl.$parsers.unshift(function(viewValue) { | |
| console.log(ctrl.$error.email); | |
| Contestant.checkUniqueEmail(viewValue).then( | |
| function (response) { | |
| ctrl.$setValidity('emailUnique', true); | |
| return viewValue; | |
| }, | |
| function (data) { | |
| ctrl.$setValidity('emailUnique', false); | |
| console.log(viewValue); | |
| return undefined; | |
| }); | |
| }); | |
| } | |
| } | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment