Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How to pass a call for example:

http://localhost/conn.php?geotable=INFR&geomfield=the_geom&srid=&fields=&parameters=&orderby=&sort=&limit=&offset=

into the javascript variable? Result of this call is string (geoJSON format lines).

Please help, I don't know what to try anymore!

share|improve this question
you want to extract the url parameters from a string? – Dagon yesterday
3  
I think you need to explain yourself better, you want to save the result of that call on a variable (like ajax) or access the getvariables in the url with javascript like you would in php? – Mark E yesterday
yes, I want to save result of that call into a javascript variable. – boofighter yesterday
"I don't know what to try anymore" - perhaps you should tell us what you have already tried. – Moshe Katz yesterday

closed as not a real question by Dagon, Fabio, Ken - Abdias Software, tkanzakic, Kristoffer S Hansen yesterday

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

2 Answers

There are many ways you could do this. Here are a few options:

(Note: For purposes of demonstration, these examples assume that your PHP script returns the GeoJSON Spec Example Code.)

  • AJAX: For doing AJAX calls, I recommend using a library, like jQuery, because it means you don't have to worry about all of the peculiarities of different browser versions. If you use jQuery, the function you need to use is $.getJSON() (see docs for more examples). Your call might look like this:

    var path = http://localhost/conn.php?geotable=INFR&geomfield=the_geom&srid=&fields=&parameters=&orderby=&sort=&limit=&offset=
    
    var result;
    $.getJSON(path, function(data) {
        result = data;
    });
    

    This will receive the GeoJSON data from the server and copy it to the result variable.

  • If you can modify the server-side script, the easiest way to do what you want is probably with JSONP. Modify the PHP code on the server so it takes a callback parameter and returns the JSON data wrapped inside a function with that name. For example, if you set callback=process, the server will return the following:

    process(
        { "type": "FeatureCollection",
          "features": [
            { "type": "Feature",
              "geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
              "properties": {"prop0": "value0"}
              },
        .....
        }
    );
    

    (Note that this is exactly the same data as the sample linked above, just with process( before it and ); after it.)

    All you need to get the data is some code like this:

    <script>
        var result;
        function process(received) {
            result = data;
        }
    </script>
    <script src="http://localhost/conn.php?callback=process&geotable=INFR&geomfield=the_geom&srid=&fields=&parameters=&orderby=&sort=&limit=&offset="></script>
    
share|improve this answer
Ok, let me be honest, I'm beginner with javascript and php and I need to store result of that call which is some points in geojson format, something like this feature collection: geojson.org/geojson-spec.html#id14 and save that data into some variable, like it's written var result = { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, "properties": {"prop0": "value0"} }, So that text needs to be stored in variable 'result' for example. That text is RESULT of that call. – boofighter yesterday
@boofighter I updated my examples. – Moshe Katz yesterday

If you want your GET variables available in javascript you could do something like this.

<script>
<?php 
echo 'var phpData = ' . json_encode($_GET);
?>
</script>

Make sure this code is before the part where you start using your javascript. You can call your variable in javascript like this:

phpData.var

For example this part of your get ?geotable=INFR would be called in javascript using phpData.geotable

Hope this helps you out.

share|improve this answer
I think he wants the JSON content from the URL, not the parts of the query string. – Moshe Katz yesterday
Ah i see he want's to call the url and then pass the return value to javascript again. Well it looks like conn.php is a local script he could apply the same tactic. Or use a curl request and catch the result then json encode the result into a js object. – Michael yesterday
Please read my comment up.. – boofighter yesterday

Not the answer you're looking for? Browse other questions tagged or ask your own question.