-1
<script>
var x = document.getElementById("latitude");
var y = document.getElementById("longitude");
function getLocation() 
{
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
        } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
        y.innerHTML = "Geolocation is not supported by this browser.";
        }
}

function showPosition(position) 
{
    x.innerHTML = "Latitude: " + position.coords.latitude;
    y.innerHTML = "Longitude: " + position.coords.longitude;
}

I need to store the X and Y value in variable in the data in SQL database and also display inside the php.

3 Answers3

1

Send an Ajax request to your php file.

var x = document.getElementById("latitude");
var y = document.getElementById("longitude");
jQuery.ajax({
        url:  "path/to/file/script.php",
        type:'POST',
        data:{
            lat:x,
            lng:y
        },
        success:function(data){

        },
        error: function(err){
            jQuery('.errorText').fadeIn();
        }
    });

Then handle values in php with $_POST['lat'] and $_POST['lng'], and query your php script to insert data to db , with mysql or mysqli.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO locations(lat, lng)
VALUES (".$_POST['lat'].", ".$_POST['lng'].")";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
Anna Gabrielyan
  • 2,120
  • 3
  • 28
  • 48
0

Well, JavaScript lives in the browser of the user, and PHP/SQL on the backend (in your control I presume).

This means you need to somehow submit those values to the backend. Three most common ways of doing so:

Then, handle such a request in PHP code and store the values in the database.

Community
  • 1
  • 1
kamituel
  • 34,606
  • 6
  • 81
  • 98
0

You can use ajax to pass the value and insert in the database. If you don't want to use ajax then you can store the value of latitude and longitude in the hidden type using the javascript. and then onsubmit pass the value of the hidden type and insert the value in the database.

var latitude = document.getElementById("latitude").value;
var longitude = document.getElementById("longitude").value;
var datastring = 'latitude='+latitude+'&longitude='+longitude ; // get data in the form manual 
jQuery.ajax({
            type: "POST", // type
            url:"ajax.php", // request file the 'check_email.php'
            data: datastring, // post the data
            async: false,
            success: function(responseText) { // get the response

            } // end success
    }).responseText; // ajax end

In the ajax file you can get the value of the latitude and longitude by $_POST['latitude'] and $_POST['longitude'] and then use that value to insert it into the database.

Riken Shah
  • 87
  • 10