Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6660139
  • 博文数量: 1159
  • 博客积分: 12444
  • 博客等级: 上将
  • 技术积分: 12570
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 21:34
文章分类

全部博文(1159)

文章存档

2016年(126)

2015年(350)

2014年(56)

2013年(91)

2012年(182)

2011年(193)

2010年(138)

2009年(23)

分类: Web开发

2016-03-16 22:34:23

http://www.keyboardninja.eu/webdevelopment/a-simple-search-with-angularjs-and-php

Recently I found AngularJS, a so called “Superheroic Javascript MVC framework” by Google?.
I must say I am impressed by its simple, readable and quick way of working.

This framework is smart, very smart. Go have a look when you’re ready on the site. Especially the basics tutorial is worth watching for beginners.

I have created a simple search form with a PHP back end and AngularJS to handle the request and response.
It’s divided in three elements:

  • The form
  • The AngularJS script
  • The server-side script

You can copy paste the source code from below or have a look at this .

The Form (searchform.htm)


点击(此处)折叠或打开

  1. <!DOCTYPE html>
  2. <html ng-app>
  3. <head>
  4. <title>Search form with AngualrJS</title>
  5.     <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
  6.     <script src=""></script>
  7.     <script src="search.js"></script>
  8. </head>

  9. <body>

  10.     <div ng-controller="SearchCtrl">
  11.     <form class="well form-search">
  12.         <label>Search:</label>
  13.         <input type="text" ng-model="keywords" class="input-medium search-query" placeholder="Keywords...">
  14.         <button type="submit" class="btn" ng-click="search()">Search</button>
  15.         <p class="help-block">Try for example: "php" or "angularjs" or "asdfg"</p>        
  16.     </form>
  17. <pre ng-model="result">
  18. {{result}}
  19. </pre>
  20.    </div>
  21. </body>

  22. </html>

The AngularJS script (search.js)


点击(此处)折叠或打开

  1. function SearchCtrl($scope, $http) {
  2.     $scope.url = 'search.php'; // The url of our search
  3.         
  4.     // The function that will be executed on button click (ng-click="search()")
  5.     $scope.search = function() {
  6.         
  7.         // Create the http post request
  8.         // the data holds the keywords
  9.         // The request is a JSON request.
  10.         $http.post($scope.url, { "data" : $scope.keywords}).
  11.         success(function(data, status) {
  12.             $scope.status = status;
  13.             $scope.data = data;
  14.             $scope.result = data; // Show result from server in our <pre></pre> element
  15.         })
  16.         .
  17.         error(function(data, status) {
  18.             $scope.data = data || "Request failed";
  19.             $scope.status = status;            
  20.         });
  21.     };
  22. }

The server-side script (search.php)


点击(此处)折叠或打开

  1. <?php
  2. // The request is a JSON request.
  3. // We must read the input.
  4. // $_POST or $_GET will not work!

  5. $data = file_get_contents("php://input");

  6. $objData = json_decode($data);

  7. // perform query or whatever you wish, sample:

  8. /*
  9. $query = 'SELECT * FROM
  10. tbl_content
  11. WHERE
  12. title="'. $objData->data .'"';
  13. */

  14. // Static array for this demo
  15. $values = array('php', 'web', 'angularjs', 'js');

  16. // Check if the keywords are in our array
  17. if(in_array($objData->data, $values)) {
  18.     echo 'I have found what you\'re looking for!';
  19. }
  20. else {
  21.     echo 'Sorry, no match!

That should do it!











阅读(1139) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~