[OpenLayers-Users] help with passing a var from OL to PHP

Nick Whitelegg Nick.Whitelegg at solent.ac.uk
Tue Nov 8 05:19:31 EST 2011


I just use loadURL for this sort of thing, eg.

OpenLayers.loadURL ('script.php', parameters, null, successCallback, failureCallback );

Then in the successCallback you could do something like (assuming you're using the Prototype library,
I'm not sure whether openlayers has its own json evaluation function but this is how you do it with
prototype):

function successCallback(xmlhttp)
{
   var json = xmlhttp.responseText.evalJSON();
    ...
}

Also is there a need for the php to be fetching the json from another file? You could just generate the json directly from the php as follows (apologies if I'm misunderstanding what you're trying to do....)

<?php
header("Content-type: application/json");

$dataToBeEncoded = [set up your data structure];

echo json_encode($dataToBeEncoded);

?>


Nick




-----openlayers-users-bounces at lists.osgeo.org wrote: -----
To: openlayers-users at lists.osgeo.org
From: Jason.Woolard at noaa.gov
Sent by: openlayers-users-bounces at lists.osgeo.org
Date: 07/11/2011 10:05PM
Subject: [OpenLayers-Users] help with passing a var from OL to PHP

hi,

I have a simple base map and I'm clicking the map to retrieve the lat/lons that will be fed into a url to retrieve a json file. I'm having trouble returning the url I want to send via PHP. I'm almost certain the problem is in the OpenLayers.Request but the docs and examples haven't helped me through this one. Can anyone see what my problem is here? 

Thanks in advance!

Relevant parts of code below:
.....
map.events.register("click", map, function(e) {
      var position = map.getLonLatFromPixel(e.xy);
      position.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));			  
      var mouselat = position.lat;
      var mouselon = position.lon;
      var geturl = 'http://locationofthejson.com/'+mouselat+','+mouselon+'.json';
      // console.log(geturl);  This prints the url ok!


       var request = OpenLayers.Request.POST({
	                       url: "http://localhost:8888/mypage.php",
			    	data: geturl,
				headers: {
					 "Content-Type": "text/plain"
				},
				success: function(request) {
						alert(request.responseText);
				}
			  });
          });

 <?php
	
	if (isset($_GET['geturl'])) { $get_url = $_GET['geturl'];}
        $json_string = file_get_contents($get_url);
	$parsed_json = json_decode($json_string);

        echo 'console.log("'.$get_url.'");';  // Nothing returned here!
?>
_______________________________________________
Users mailing list
Users at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/openlayers-users


More information about the Users mailing list