Page 1 of 1

AngularJs : ตัวอย่าง ค้นหา ด้วย AngularJS Ajax เเละ PHP

Posted: 31/05/2016 3:55 pm
by thatsawan

Code: Select all

<div ng-app="AutocompleteApp" ng-controller="AutocompleteCtrl">
    <dl style="clear: left;" id="panel_tag">
        <div class="rh_tag_cat">
            <input type="text" ng-model="keywords"  ng-change="search()">
        </div>
        <div>
            <p ng-show="loading">Loading...</p>
            <p ng-hide="loading">Response: [[response]]</p>
            <p ng-hide="loading">writed: [[keywords]]</p>
        </div>
    </dl>
</div>
ไฟล์ JS

Code: Select all

    var app = angular.module('AutocompleteApp', []);

    app.config(function ($interpolateProvider) {
        $interpolateProvider.startSymbol('[[');
        $interpolateProvider.endSymbol(']]');

    })


    app.service('searchService', ['$http', function ($http) {
            return {
                search: function (keywords) {
                    return $http.post('{URL_JSON_SHOW_SEARCH}', {"keywords_search": keywords});
                }
            }
        }]);

    app.controller('AutocompleteCtrl', ['$scope', 'searchService', function ($scope, searchService) {
            $scope.search = function () {
                searchService.search($scope.keywords).then(function (response) {
                    console.log($scope.response);
                    $scope.response = response.data;
                });
            };
        }]);

 
URL_JSON_SHOW_SEARCH = http://localhost/***/app.php/m_xxxxx/search


ไฟล์ php

Code: Select all

 public function search() {

        $data = file_get_contents("php://input");
        $objData = json_decode($data);
        
        $sql = 'SELECT * FROM ' . $this->m_tag_category . " WHERE  tag_name LIKE '%".$objData->keywords_search."%' ORDER  BY tag_name ASC ";
        
        $query = $this->db->sql_query($sql);
        $data = array();
     
        while ($row = $this->db->sql_fetchrow($query)) {
            $data[$row['id']]['name'] = $row['tag_name'];
            $data[$row['id']]['count'] = $row['count'];
            $data[$row['id']]['confederation'] = 0;
            $data[$row['id']]['rank'] = $row['count'];
            $data[$row['id']]['id'] = $row['id'];
        }

        $json_response = new \phpbb\json_response;
        $json_response->send(array($data));
    }
2016-05-31_15-54-19.png
2016-05-31_15-54-19.png (81.98 KiB) Viewed 834 times