简体   繁体   English

使用angularjs和php从数据库中选择的数据未显示到下拉列表中

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

Here I'm tring to get data from database and show it in dropdown list and when selecting it will be stored in database, i'm using angularjs and php but unfortunatly m not getting anything in dropdown list... please help me out.. Thanks in advance.. 在这里,我正试图从数据库中获取数据并将其显示在下拉列表中,并且当选择将其存储在数据库中时,我正在使用angularjs和php,但不幸的是我没有在下拉列表中获取任何数据...请帮助我。 。 提前致谢..

This is my html code. 这是我的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>

This is my service 这是我的服务

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;
            });
        }
    };
}]);

This is my controller 这是我的控制器

$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);
});
}

This is my php code 这是我的PHP代码

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

    $query = "SELECT Sites FROM sitemaster";

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

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

?>

Change your factory to look like this: 将工厂更改为如下所示:

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;
        }
    };
}]);

I believe this should solve your problem. 我相信这应该可以解决您的问题。 also, remove the $http.post from your controller, if you have a factory, put all the api/ajax related code there, like you have done with the get requests. 同样,从控制器中删除$http.post ,如果您有工厂,则将所有与api / ajax相关的代码放在此处,就像对get请求所做的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM