a simple search with AngularJS and PHP

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) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值