0

PHP & jQuery "$.post" are making troubles. I'm posting a few variables to a PHP-File.

The jQuery is:

navigator.geolocation.getCurrentPosition(saveGeoLocation);
function saveGeoLocation(position) {
   var latitude = position.coords.latitude;
   var longitude = position.coords.longitude;   

   $.post("__g.php", { 
      latitude: latitude, 
      longitude: longitude 
   }).done(function(data) {
      console.log("Data Loaded: " + data);
   });

}

The PHP is:

$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
echo "Position output: " . $latitude . $longitude;

The console is showing the echo correctly with all the information sent via jQuery. However on the page itself, the PHP-echo is just echoing the content inside the quotation marks but not the variables content.

The PHP is in a single file but imported to a bigger file via include().

(This is a reduced example, there could be a typo.)

Thanks for your wisdom!

----------EDIT--------:

My problem might be based on a rookie mistake. Is it possible to include a php file AND at the same time send data to it so that it outputs the data in the place you want it to? Like:

<somehtml>
<somejquery> *--> is retrieving Geolocation-coordinates and posting long/lat to "__g.php"*

<?php
    <somephp>
    include("__g.php"); *--> is echoing the full API-url containing the long/lat values from the jQuery-post*
    <someforeach> *--> for the received API-json*
?>
  • check in XHR under network tab of console, is it sending data or not – Ahmed Sunny Sep 11 '18 at 13:50
  • Hi, I'm getting the following response: https://imghost.eu/2Zy/Bildschirmfoto_2018-09-11_um_15.54.14.png –  Sep 11 '18 at 13:56
  • what exactly are you trying to achieve here? – 95faf8e76605e973 Sep 11 '18 at 13:57
  • click on it and scroll to end, see if data is there, lattitude and longitude, under form data – Ahmed Sunny Sep 11 '18 at 13:58
  • @AhmedSunny, yes form data is there. https://imghost.eu/2Zz/Bildschirmfoto_2018-09-11_um_16.05.37.png –  Sep 11 '18 at 14:06
  • @95faf8e76605e973, the long/lat data is being sent to an API and retrieving some information about the corresponding location. –  Sep 11 '18 at 14:07
  • 2
    @ToE ok but you stated _However on the page itself, the PHP-echo is just echoing the content inside the quotation marks but not the variables content._ are you are trying to directly run the page with the php script? – 95faf8e76605e973 Sep 11 '18 at 14:14
  • 1
    This seems to come up all the time on StackOverflow. You cannot just navigate to the PHP page; that will be a `GET` request and it won't have any `$_POST` parameters – Phil Sep 11 '18 at 14:38
  • 1
    i don't think you need ajax for what your are ultimately trying to achieve. a simple form should do the trick. ajax is used so that you can display the data on the page that made the ajax request – 95faf8e76605e973 Sep 11 '18 at 15:12
  • But unfortunately the geodata only retrievable through Javascript. And I need to pass that location to some php so that the php is able to output the foreach for each parameter based on the output of an API-url. –  Sep 11 '18 at 15:18

1 Answers1

-1

change the following from:

   $.post("__g.php", { 
  latitude: latitude, 
  longitude: longitude 
  }).done(function(data) {
     console.log("Data Loaded: " + data);
  });

to

   $.ajax({
       url: "__g.php",
       type: "POST",
       dataType "text",
       data: {
          latitude: latitude, 
          longitude: longitude
       },
       success: function(data) {
         console.log("Data Loaded: " + data);
       },
       error: function(data) {
          console.log("Error: an error occurred somewhere");
       } 
  });

change your php code from:

$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
echo "Position output: " . $latitude . $longitude;

to

if (isset($_REQUEST['latitude']) && isset($_REQUEST['longitude'])) {
   $latitude = $_REQUEST['latitude'];
   $longitude = $_REQUEST['longitude'];

   print "Position output: " . $latitude . $longitude;
} else {
    header('HTTP/1.1 500 Internal Server');
    header('Content-Type: application/json; charset=UTF-8');
}
JamesBond
  • 312
  • 2
  • 17
  • @ToE , you will have to load the JQuery library first. – JamesBond Sep 11 '18 at 14:03
  • Hi, thanks for your post! Unfortunately it's returning {"message":"ERROR","code":"someerror"} –  Sep 11 '18 at 14:12
  • @ToE that means that the post has not been set, try setting a test value for both variables and see if it returns success – JamesBond Sep 11 '18 at 14:14
  • Unfortunately the test values don't work either. The error data returned to the jQuery/console says [Object Object]. –  Sep 11 '18 at 14:25
  • @ToE i have updated my answer, that should solve the error – JamesBond Sep 11 '18 at 14:34
  • `dataType "json"` <- the response is **not** JSON – Phil Sep 11 '18 at 14:41
  • @Phil Oops x_x, updated – JamesBond Sep 11 '18 at 14:43
  • Thanks for your help @JamesBond, it's returning data now. I guess the reason why it's not echoing on the page itself, is because I'm including the php file I'm sending the data to, in the main php file (see edit up top). –  Sep 11 '18 at 15:06