使用PHP

时间:2016-01-13 11:25:32

标签: javascript php ionic-framework ionic

我正在尝试使用javascript将我收到的数据发送到localhost但是当我将其构建为android应用程序时,PHP文件不会运行。我已经尝试在构建它之前在XAMP中正常运行它并且它似乎PHP连接即使数据没有被发送,但是在将它构建为离子android应用程序之后它甚至都没有连接。这里出了什么问题?

的index.html

     <?php
        include "main.php";

    ?>
    <!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
<!-- START OF GEOLOCATION -->


<center><div class="round-button"><div class="round-button-circle"><a onclick= "getLocation()" class="round-button">HELP</a></div></div></center>

<p id="demo"></p>
<script src="js/jquery.js"></script>




<script>

var glob_latitude = '';
var glob_longitude = '';

var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.watchPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";}
    }


///send to ip
function showPosition(position) {
    x.innerHTML="Latitude: " + position.coords.latitude +
    "<br>Longitude: " + position.coords.longitude;

    glob_longitude = position.coords.longitude;
    glob_latitude = position.coords.latitude;




   $.post( "main.php", { latitude: glob_latitude, longitude: glob_longitude } );

}



</script>
<!-- END OF GEOLOCATION -->
    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->


    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter" background="css/style.css">


  </body>
</html>

Main.php

  <?php
echo "ok";

//$dbConnection = mysqli_connect("160.153.162.9", "Musab_Rashid" , "zaq123wsx" ,"Musab_Rme");
$dbConnection = mysqli_connect("localhost", "root" , "" ,"info");
echo "connected";
if($dbConnection)
    {
        echo "connected";
        if(isset($_POST['latitude']) and isset($_POST['longitude'])){
            $latitude = $_POST['latitude'];
            $longitude = $_POST['longitude'];

            if($latitude != '' and $longitude != '')
                $query = mysqli_query("INSERT INTO info VALUES (NULL, '{$latitude}', '$longitude')");

        }

    }
else
    die();

mysqli_close($dbConnection);
?>

1 个答案:

答案 0 :(得分:1)

好吧,有几件事:

  1. 当您在localhost上执行php时,无法使用php逻辑制作离子应用程序。 php必须在外部服务器上执行。原因很简单,当您导出应用并在手机上试用时,应用程序无法访问您的本地主机。更具体一点:

     <?php
    include "main.php";
    ?>
    
  2. 结合ajax请求:

     $.post( "main.php", { latitude: glob_latitude, longitude: glob_longitude } );
    
    1. 我在评论中试图对您说,您的数据流是这样的:
    2. 应用程序通过ajax请求发送数据 - &gt; PHP执行传入数据 - &gt; php echo的json对象或字符串 - &gt;检索字符串或json对象 - &gt;向用户显示数据

      看看这个来源,帮助我开始。 http://www.nikola-breznjak.com/

      古德勒克!