将javascript变量发布到php

时间:2016-01-14 09:35:12

标签: javascript php sql

我正在尝试将一个javascript变量发布到PHP,以便我可以在我的localhost上更新数据库。 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);
?>

3 个答案:

答案 0 :(得分:0)

您将数据发布到PHP的哪个位置?我只看到一个'&lt; a onclick =“getLocation()”',并且在函数中你获得了位置,但是你就停在那里。 您的函数需要调用有关数据更改的PHP脚本:

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

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

    var data = {
        'longitude': position.coords.longitude,
        'latitude': position.coords.latitude
    } ;

    // now tell PHP via Ajax
    $.post ('myphp.php', data, function (ret, status) { alert (ret) ; }) ;
}

因为我发送了一个json对象,你可能需要调整一下PHP脚本。

答案 1 :(得分:0)

查看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
      }
    }
  );
}

答案 2 :(得分:0)

到目前为止,您的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);

}