简体   繁体   English

使用PHP从Ionic框架发布数据

[英]Posting data from Ionic framework using PHP

I am trying to send the data I've received using javascript to the localhost but the PHP file doesn't run when I build it as an android application.I've tried running it normally in XAMP before building it and it seems like the PHP connects even tho the data doesn't get sent, however after building it as an ionic android application it doesn't even connect. 我正在尝试将使用javascript接收到的数据发送到localhost,但是当我将其作为android应用程序构建时,PHP文件无法运行。在构建之前,我已经尝试在XAMP中正常运行它,似乎PHP甚至连数据都不会发送出去,但是在将其构建为离子Android应用程序后甚至连不上。 What is going wrong here? 这是怎么了?

Index.HTML 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 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);
?>

Allright, couple of things: 好吧,几件事:

  1. It is not possible to make an ionic app with php logic when u execute the php on your localhost. 当您在本地主机上执行php时,无法使用php逻辑制作离子应用程序。 The php must be executed on a external server. php必须在外部服务器上执行。 The simple reason is, when you export your app and try it on your phone, the application can't access your localhost. 原因很简单,当您导出应用程序并在手机上尝试时,该应用程序无法访问您的本地主机。 To be more specific: 更加具体:

      <?php include "main.php"; ?> 

In combination with the ajax request: 结合ajax请求:

 $.post( "main.php", { latitude: glob_latitude, longitude: glob_longitude } );
  1. What i tried to say to you in my comment, is that your dataflow you look like this: 我在评论中试图对您说的是,您的数据流如下所示:

App sends data by ajax request -> PHP executes incoming data -> php echo's json object or string -> retrieve string or json object -> show data to user 应用通过ajax请求发送数据-> PHP执行传入数据-> php echo的json对象或字符串->检索字符串或json对象->向用户显示数据

Have a look at this source, helped me to get me started. 看看这个资源,帮助我入门。 http://www.nikola-breznjak.com/ http://www.nikola-breznjak.com/

Goodluck! 祝好运!

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

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