Jump to content

POST Method. Post javascript to php script


davefootball123

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>

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'
})

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.