a simple search with AngularJS and PHP

钱修雅
2023-12-01
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 AngularJS 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 DEMO.

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="http://code.angularjs.org/angular-1.0.0.min.js"></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!











<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(12) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
 类似资料:

相关阅读

相关文章

相关问答