dongshi9407 2016-03-06 14:53
浏览 36

如何使用$ http.get从同一页面获取php数据

hope you fine and well ,

i want to ask if it possible to fetch data using $http.get from the same page , to go deep in details :

i have a php file called (MY_FILE.php) that contains : PHP,HTML and SCRIPT codes , in the PHP section i executed sql select statment, and im trying to fetch the selected data in the script to use it in the html part , is this possible ?!

php part :

<?php
$connect = mysqli_connect("localhost", "root", "", "database");
$result = mysqli_query($connect, "select * from table");
$data = array();
while ($row = mysqli_fetch_array($result)) {
    $data[] = $row;
}
print json_encode($data);

?>

script part :

fetch.controller('dbCtrl', ['$scope', '$http', function ($scope, $http) {
        $http.get("MY_FILE.php")
            .success(function(data){
                $scope.data = data;
            })
            .error(function() {
                $scope.data = "error in fetching data";
            });
    }]);

when i use any of the selected data in the html section nothing appear and stay blank.

any idea ?!

  • 写回答

1条回答 默认 最新

  • douang4294 2017-07-07 11:41
    关注

    The code is right, but the issue with using $http.get for the same page is when you use print json_encode($data); for returning response to the $http call, rather then returning call to $http, it prints the data on the page. So it returns nothng. So its better to use another page and using another page will be more user friendly as well because the page won't refresh for fetching the results. Secondly, because of the asynchronous nature of $http, the program will keep on executing and it won't be waiting for the php code to run and showing result. As soon as the php code is completely executed it will show the result.

    And if you want everything to happen on same page you can simply use

    <?php
        if(isset($_POST['varname']))
        {}
        ?> and
        <html>
        <form action="" method="POST" > 
    
        </form></html>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度