0

Is there any easy way to pass data from JavaScript to PHP without using the Query String Method.

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    }
}
function showPosition(position) {
    document.getElementById("getlat").value = position.coords.latitude;
    document.getElementById("getlon").value = position.coords.longitude;
}

HTML

<input type="hidden" id="getlat" name="getlat" value="<?php echo $_SESSION['polat']; ?>" /> 
<input type="hidden" id="getlon" name="getlon" value="<?php echo $_SESSION['polon']; ?>" />

PHP Code

$lat = '-27.486204'; // Should get latitude from JS here.
$long = '152.994962'; // Should get longitude from JS here.

    mysqli_query("SELECT * From `table_nme` where `latitude`='.$lat.' AND `longitude`='.$long.'");

I know how to do this in Query String Process but I need it without Query String Process.

3
  • Simple answer: Ajax. Commented Aug 24, 2014 at 5:42
  • There's a few options, but what's wrong with a query string? Do you have some particular limitation that might indicate what option is best for your situation? Also, the current method for building your SQL query is vulnerable to SQL injection attacks if you support client data and simply insert it into the SQL. Commented Aug 24, 2014 at 5:44
  • One simple option is to use cookies. (probably not the best option) Commented Aug 24, 2014 at 5:46

1 Answer 1

1

You can use ajax (jquery) to send your data to server.

JS

$.ajax({url:"yourfile.php",type:"POST",async:false,
   data:{getlat:$("#getlat").val(),getlon:$("#getlon").val()}
});

PHP

$lat = $_POST["getlat"];
$lon = $_POST["getlon"];
...

Another solution is to use cookies. Personally i use this to get/set my cookies:

JS

....
$.cookie("data",{getlat:$("#getlat").val(),getlon:$("#getlon").val()});

PHP

$data = json_decode($_COOKIE["data"]);
$lat = $data["getlat"];
$lon = $data["getlon"];
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.