簡體   English   中英

使用angularjs和php從數據庫中選擇的數據未顯示到下拉列表中

[英]selected data from the database is not showing into dropdown list using angularjs and php

在這里,我正試圖從數據庫中獲取數據並將其顯示在下拉列表中,並且當選擇將其存儲在數據庫中時,我正在使用angularjs和php,但不幸的是我沒有在下拉列表中獲取任何數據...請幫助我。 。 提前致謝..

這是我的html代碼。

<select class="form-control" ng-model="bookInfo.site1" placeholder="sites">
            <option ng-repeat="site in bookInfo.site1" value="{{site.Sites}}">{{site.Sites}}</option>
        </select>
<select class="form-control" ng-model="bookInfo.site2" placeholder="sites">
            <option ng-repeat="site in bookInfo.site2" value="{{site.Sites}}">{{site.Sites}}</option>
        </select>

這是我的服務

app.factory('SiteService', ['$http', '$q', function($http, $q){
    return {
        getSite1: function(){
            return $http.get('endpoints/selectsite.php').then(function(result) {
                return result.data;
            });
        },
        getSite2: function(animal){
            return $http.get('endpoints/selectsite.php').then(function(result){
                return result.data;
            });
        }
    };
}]);

這是我的控制器

$scope.bookInfo = {

    sites1: [],
    sites2: []
}

//functions

SiteService.getSite1().then(function(data){
    $scope.bookInfo.sites1 = data;
});

SiteService.getSite2().then(function(data){
    $scope.bookInfo.sites2 = data;
});

$scope.bookVisit = function(){

var data = {

    site1: $scope.bookInfo.site1,
    site2: $scope.bookInfo.site2
}
$http.post("endpoints/book.php", data).success(function(response){
    console.log(response);
    $state.go("application");
}).error(function(error){
    console.error(error);
});
}

這是我的PHP代碼

<?php
    include("../connection.php");

    $query = "SELECT Sites FROM sitemaster";

    $rs = $db->query($query);

    while($row = $rs->fetchAll()){
        $data[] = $row;
    }
    print json_encode($data);

?>

將工廠更改為如下所示:

app.factory('SiteService', ['$http', '$q', function($http, $q){
    return {
        getSite1: function(){
            var do = $q.defer();
            $http.get('endpoints/selectsite.php').then(function(){
                do.resolve;
            },
            function(){
                do.reject;
            });
            return do.promise;
        },
        getSite2: function(animal){
            var do = $q.defer();
            $http.get('endpoints/selectsite.php').then(function(){
                do.resolve;
            },
            function(){
                do.reject;
            });
            return do.promise;
        }
    };
}]);

我相信這應該可以解決您的問題。 同樣,從控制器中刪除$http.post ,如果您有工廠,則將所有與api / ajax相關的代碼放在此處,就像對get請求所做的那樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM