1

I wrote a JavaScript function to convert GeoJson data to WKT format. It works when i get the input value in the javascript code directly. But I don't know how to get the input from the php and send it back.

Here is the php code:

<?php
  $geojson=file_get_contents("clipfeature.geojson");
  $WKT = $_POST['wkt'];
  echo ($WKT);
?>

So it gets geojson data from a file and I want to receive the converted WKT code from the Javascript function.

Please help me finish the JavaScript Code:

   function converttoWKT (){
     $.ajax({
        type: "GET",
        url: "readJson.php",
        contentType: "application/json"
    }).done(function (data) {

    var JSONObject = How to give the value from PHP to this Variable;

    var coordinate = JSONObject.features[0].geometry.coordinates;
    var type= JSONObject.features[0].geometry.type;


    var coordinate1 = "";
    var coordinate2 = "";
    for (var i=0; i< coordinate[0].length; i++) {
     coordinate1= coordinate[0][i][0]+" "+coordinate[0][i][1];
     coordinate2=coordinate1+","+coordinate2;
    }

    var WKT= "\""+ type + "((" + coordinate2;
        WKT=WKT.substring(0,WKT.length-1);
        WKT=WKT+"))\"" 

    sendback ( );

}); 

    function sendback(){$.post("readJson.php",
    {'wkt':How to send the value of var WKT back to php 'wkt'
    });
    }
2
  • have try json_encode($_POST['wkt']); in your php script? Commented May 21, 2013 at 17:44
  • 1
    Also set your header in the php file. The javascript is expecting json so you should give it json. use: header("Content-Type: application/json; charset=utf-8"); Commented May 21, 2013 at 17:46

2 Answers 2

1

Basically:

var jsVar= "<? echo $myVariable_value_goes_in_here; ?>";

or, as mentioned above:

var JSONObject =  <? echo json_encode($WKT); ?>;  
                  // this NEED TO BE json, otherwise syntax error in JS!
Sign up to request clarification or add additional context in comments.

Comments

1

Since you already know your return data will be JSON, you can just use $.getJSON() for convenience. This is a $.get() request paired with JSON.parse(). With $.getJSON(), the JSON is parsed on response. To send the data back, just use use jQuery's $.post().

Here's an edited version of your code.

function converttoWKT() {
  $.getJSON('clipfeature.geojson', function(data) {
    var coordinate = data.features[0].geometry.coordinates;
    var type = data.features[0].geometry.type;

    var coordinate1 = '';
    var coordinate2 = '';
    for(var i = 0; i < coordinate[0].length; i++) {
      coordinate1 = coordinate[0][i][0] + ' ' + coordinate[0][i][1];
      coordinate2 = coordinate1 + ',' + coordinate2;
    }

    var WKT = '"' + type + '((' + coordinate2;
    WKT = WKT.substring(0, WKT.length - 1);
    WKT = WKT + '))"'

    sendback(WKT);
  });
};

function sendback(data) {
  $.post('readJson.php', {
    'wkt': data
  });
};

1 Comment

Thanks. I solved my problem. At first, the php still show error: Undefined index. Because I refresh the website but didn't refresh the javaScrip. At the end I just push "Shift"+ refresh, updating the javascript and it works finally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.