Javascript 使用PHP从Ionic framework发布数据
我正在尝试将使用javascript接收到的数据发送到本地主机,但当我将其构建为android应用程序时,PHP文件不会运行。在构建它之前,我尝试在XAMP中正常运行它,似乎PHP会连接,即使数据未被发送,然而,在将其构建为离子安卓应用程序之后,它甚至连连接都没有。这里出了什么问题 Index.HTMLJavascript 使用PHP从Ionic framework发布数据,javascript,php,ionic-framework,ionic,Javascript,Php,Ionic Framework,Ionic,我正在尝试将使用javascript接收到的数据发送到本地主机,但当我将其构建为android应用程序时,PHP文件不会运行。在构建它之前,我尝试在XAMP中正常运行它,似乎PHP会连接,即使数据未被发送,然而,在将其构建为离子安卓应用程序之后,它甚至连连接都没有。这里出了什么问题 Index.HTML <?php include "main.php"; ?> <!DOCTYPE html> <html> &l
<?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>
帮助
var全球纬度=“”;
var glob_经度=“”;
var x=document.getElementById(“演示”);
函数getLocation(){
if(导航器.地理位置){
导航器、地理位置、监视位置(showPosition);
}否则{
x、 innerHTML=“此浏览器不支持地理位置。”;}
}
///发送到ip
功能显示位置(位置){
x、 innerHTML=“纬度:”+position.coords.Latitude+
“
经度:”+position.coords.Longitude;
glob_经度=position.coords.longitude;
全球纬度=位置坐标纬度;
$.post(“main.php”,{纬度:glob_纬度,经度:glob_经度});
}
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);
?>
好吧,有两件事:
<?php
include "main.php";
?>
祝你好运 Ionic是一个移动框架,因此我假设您将在移动设备上运行该应用程序。这意味着您需要将应用程序与服务器端分离(您需要在某个公共web主机上运行该应用程序),您必须在app.jsp中获取返回的值,但在未连接到本地主机时不会出现错误,这是在移动结果上运行此代码时的明显结果。这意味着应用程序无法运行PHP代码,还是我错了?@Lars,请告诉我我的答案对你有用吗?或者你有任何更具体的问题吗?纬度和经度没有发布到main.php,即使在将其设置到外部服务器时也是如此。你可以将你的问题帖子编辑到现在的设置中吗?你应该尝试设置我给你的示例。查看链接,看看你是否能让它起作用,并从这一点向前推进。这需要一些努力,但一旦你能让它发挥作用,它将是值得的!
$.post( "main.php", { latitude: glob_latitude, longitude: glob_longitude } );