【问题标题】:posting javascript variable to php将 javascript 变量发布到 php
【发布时间】:2016-04-19 13:37:29
【问题描述】:

我正在尝试将 javascript 变量发布到 PHP,以便我可以更新本地主机上的数据库。 PHP似乎连接到数据库,但数据库没有更新。

<?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="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 } postPosition(position); 

}

function postPosition(position){
    $.post('main.php',{
    latitude: position.coords.latitude,
    longitude: position.coords.longitude,
    },function(phpScriptResponse){
        if(phpScriptResponse !== "ok"){ x.innerHTML= phpScriptResponse; }
      }
    }
  );
}

    </script>


      </body>
    </html>

这是连接后应该更新数据库的 PHP 代码...

<?php


$dbConnection = mysqli_connect("localhost", "root" , "" ,"info");

if($dbConnection)
    {
        echo "connected"; 
        if(isset($_POST['latitude']) and isset($_POST['longitude'])){
            echo "hi";
            $latitude = $_POST['latitude'];
            $longitude = $_POST['longitude'];

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

        }

    }
else
    die();

mysqli_close($dbConnection);
?>

【问题讨论】:

  • 您的代码缺少form。您正在尝试检索 $_POST['latitude'],但您从未发布过它。
  • 你想将哪些变量发送给 PHP glob_longitude = position.coords.longitude; glob_latitude = position.coords.latitude;
  • 我添加了 $.post("main.php", { latitude: glob_latitude, longitude: glob_longitude } );但还是什么都没有……

标签: javascript php sql


【解决方案1】:

到目前为止,您的 html 文件中没有表单,您可以使用 AJAX 将您的数据处理到 PHP。此时不要包含main.php文件

            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;

        var http = new XMLHttpRequest();
        var url = "main.php";
        var params = "latitude="+glob_latitude+"&longitude="+glob_longitude+"";
        http.open("POST", url, true);

        //Send the proper header information along with the request
        http.setRequestHeader("Content-type", "application/x-www-form-              urlencoded");
        http.setRequestHeader("Content-length", params.length);
        http.setRequestHeader("Connection", "close");

        http.onreadystatechange = function() {//Call a function when the state changes.
         if(http.readyState == 4 && http.status == 200) {
             alert(http.responseText);
       }
    }
  http.send(params);

}

【讨论】:

  • 我需要一台服务器吗?因为我目前正试图让它在本地主机上工作
  • 是的,您应该启动并运行服务器
  • 这是您在 PHP 中接收值的方式 $latitude = $_POST['latitude']; $longitude = $_POST['longitude'];
【解决方案2】:

看看http://api.jquery.com/jquery.post/

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;

    postPosition(position); 

}

function postPosition(position){
    $.post('url/to/your.php',{
        latitude: position.coords.latitude,
        longitude: position.coords.longitude,
    },function(phpScriptResponse){
        if(phpScriptResponse !== "connectedhihi"){
        // error
      }
    }
  );
}

【讨论】:

  • 我已经添加了函数,但是我没有看到数据库中的数据
  • 我添加了 if(phpScriptResponse !=="connectedhihi"){ x.innerHTML="error" } 它开始显示错误
  • 我建议如下:如果没有错误,请确保您的 PHP 脚本将“ok”(或其他任何内容)写入标准输出,并在出现错误时写入错误。然后可以改成if(phpScriptResponse !== "ok"){ x.innerHTML= phpScriptResponse; }
  • 代码不知何故不想超出数据库连接部分,一旦连接,之后什么都没有发生
【解决方案3】:

【讨论】:

  • 我已经提出了你提到的建议,但由于某种原因,现在它甚至根本无法获取 gps 位置
  • 不确定你的意思,getLocation() 和 showPosition() 函数没有被调用吗?这意味着您更改了某些内容以使其停止工作 - 检查 javascript 控制台以防出现错误。还是您的意思是您停止从您的设备接收 GPS 信息?再次,javascript 控制台可能有助于找出问题所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
相关资源
最近更新 更多