davefootball123 Posted February 21, 2013 Share Posted February 21, 2013 (edited) 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 February 21, 2013 by davefootball123 Quote Link to comment https://forums.phpfreaks.com/topic/274801-post-method-post-javascript-to-php-script/ Share on other sites More sharing options...
exeTrix Posted February 21, 2013 Share Posted February 21, 2013 (edited) 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 February 21, 2013 by exeTrix Quote Link to comment https://forums.phpfreaks.com/topic/274801-post-method-post-javascript-to-php-script/#findComment-1414043 Share on other sites More sharing options...
davefootball123 Posted February 22, 2013 Author Share Posted February 22, 2013 Thanks for the response...I am either doing something incorrectly or that is not what im looking for ...I think what I am looking for is setting a php variable with ajax. Quote Link to comment https://forums.phpfreaks.com/topic/274801-post-method-post-javascript-to-php-script/#findComment-1414059 Share on other sites More sharing options...
DavidAM Posted February 22, 2013 Share Posted February 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274801-post-method-post-javascript-to-php-script/#findComment-1414073 Share on other sites More sharing options...
davefootball123 Posted February 22, 2013 Author Share Posted February 22, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/274801-post-method-post-javascript-to-php-script/#findComment-1414080 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.