0
votes

My HTML script gathers the latitude & longitude and sends them to another PHP script via ajax, which inserts those values into mysql, problem is that AJAX inserts NULL values into the table. It's inserting values of "0.000000" for both Latitude & Longitude, lat & long are type float(10,6) in the DB as stated by the internet for best practice with gps coords. I'm obvious new developer please help -- HTML Script

<!DOCTYPE html>
<html>
  <head>
    <title>AJAX DATABASE</title>

    <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
    <script type="text/javascript" charset="utf-8">



    /*/ Wait for Cordova to load
    /*/
    document.addEventListener("deviceready", onDeviceReady, false);

    var watchID = null;

    // Cordova is ready
    //

    function onDeviceReady() {
        // Throw an error if no update is received every 30 seconds
        var options = {enableHighAccuracy:true, timeout: 3000, maximumAge:0};
        watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);

    }


    // onSuccess Geolocation
    //
    function onSuccess(position) {    


        var latitude = document.getElementById("lat");
        latitude.innerHTML = position.coords.latitude;
        var longitude = document.getElementById("longitude");
        latitude.innerHTML = position.coords.latitude;
        insertDB(position.coords.latitude,position.coords.latitude);
    }

    // onError Callback receives a [PositionError](PositionError/positionError.html) object
    //
    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }

    // onError Callback receives a PositionError object
    //
    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }

    // Options: throw an error if no update is received every 30 seconds.
    //
    var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 30000 });




    // Sends latitude & longitude value to php script and alerts on success

    function insertDB(latitude, longitude){
        var xttp = new XMLHttpRequest();
        xttp.onreadystatechange = function(){
            if(this.readyState == 4){
                document.getElementById('longitude').innerHTML = 
                this.responseText;
                alert("state changed by ajax")
            }

        }
        xttp.open("GET", "ConnectTest.php? 
         latitude="+latitude+"&longitude="+longitude, true);
        xttp.send();
    }




    </script>


  </head>
  <body>

    <p id="lat"></p><br>
    <p id="longitude"></p>
  </body>
</html>

and the PHP --

<?php 


    //POST VARIABLES


    $one = $_GET['latitude'];
    $two = $_GET['longitude'];

    //ESTABLISH CONNECTION 

    $servername = "localhost:3306";
    $user = "client";
    $pass = "sonic_client";

    $db_name = "sonicstrains";

    $server = mysqli_connect($servername, $user, $pass, $db_name);




    $sql = "SELECT * FROM 'users' WHERE 'latitude' = ".$two;

    $result = mysqli_query($server, $sql);

    if(!isset($_GET['latitude'])){
        echo "Nothing is set";

    }

    if(!$server){

        die("Connetion error".mysqli_connect_error());

    }else{

        if (!$result){

            $insert = "INSERT INTO users (latitude, longitude) VALUES ('$one', '$two')";
            mysqli_query($server, $insert);
            echo "Coordinates Inserted";

        }
    }


?>
1
You're putting the parameters in the URL, which means you need to use $_GET to access them. The fact that you used POST in your code is irrelevant in this case. POST params always go in the body of the request, you are send()ing an empty body. You should also look into fetch, which will make your obsolete code a lot simpler.Chris G
Not sure what is the problem. You want 0.0000? you want null?Juan Carlos Oropeza
Big help, but my longitude isn't posting for some reason. still posting 0.000, know why? I changed both params in php to $_Get['foo']Sonic Strains
@SonicStrains you are passing only latitude value in function insertDB(latitude). You need to pass both latitude and longitude value function insertDB(latitude, longitude)Sonal Borkar
I updated the function to include longitude, but only the latitude is posting stillSonic Strains

1 Answers

0
votes

Replace the xttp open and send functions with below:

    xttp.open("POST", "ConnectTest.php, true);
    xttp.send(latitude="+latitude+"&longitude="+longitude);