Jump to content

Recommended Posts

I have a php file that reads lat lon that is sent from another file. Seen below. I also have a a javascript that gets geolocation seen below the php script. How would I post the latitude longitude from the javascript to the php script for processing. I am trying to post it to widgetextract.php and from there the latitude longitude is compared with a database. The reason im using the W3C api is because it is very accurate. Any help would be great.

 

Thanks, Dave

 

 

<?php
$lat = $_POST['lat'];
$lon = $_POST['lon'];

<script type="text/javascript">
//Check if browser supports W3C Geolocation API
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} else {
alert('Browser does not support geolocation');
}


function successFunction(position) {
var mylat = (position.coords.latitude);
var mylon = (position.coords.longitude);




function errorFunction(position) {
alert('No Location Found');
}


</script>

Edited by davefootball123

Not really a PHP question more JS/jQuery but I'd use:

 

$.post({
type: "POST",
url: 'http://fqdn.com/script.php',
data: {
lat: 3623767534757634657345734567345765367,
long: 5847589534534653753476536575
},
success: function( resp ){
//do something here to inform the user everything is hunky dori
},
dataType:'json'
})

Edited by exeTrix

The code from exeTrix is using jQuery, so you will have to load the jQuery library -- see their website at jQuery API Documentation.

 

You will have to change the url parameter in that code to the url of your widgetextract.php (or whatever script you write to handle the AJAX call) in order for it to post to your PHP.

 

what I am looking for is setting a php variable with ajax.

PHP is run on the server, so it is done and gone before the page displays. So there is no way to "set a php variable with ajax". You can, however, pass data to (a new instance of) your PHP script and have it pass data (as output) back to AJAX.

Thanks for the help. I now have the js printing the code seen below before the php file is run. How would i get to grab the text inside the <p> as a variable? The javascript is printing the lat and long to the <p id>

If anyone knows how to get the paragraph with the id as a variable that would be awesome

 

Dave,

<p id="lat">latitude(variable)</p>
<p id="lon">longitude(variable)</p>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.