I have get the variable from onEachFeature function and set on a HTML tag, like this

function onEachFeature(feature, layer) {
  layer.on("click", function(e)
    var popup = e.target.getPopup();
    var content = popup.getContent();
    document.getElementById('test').textContent = content;

This is my PHP

        <h2>My Map</h2>
        <p id="test"></p>
        <div id="map"></div>
        <script src="js/new.js"> </script>

I have get the value to show on HTML but I want the variable to get to PHP? Exactly is the content from popup (content variable) to echo to PHP?


So you have an HTML textbox and you want to place the clicked features attribute into the textbox. Here I get the textbox, then set the value.

onEachFeature: function (feature, layer) {          
   layer.on('click', function (e) {     

     var a = document.getElementById("test");
| improve this answer | |
  • No, I mean, I want the value from this "e.target.feature.properties.TenCty", to pass to PHP variable. Anyway, thanks very much. Actually, I have read about ajax to send variable to PHP, but I don't know why it didn't work – Diao Vũ Jul 21 '18 at 3:20
  • Actually, your answer helps me very much, Thank you – Diao Vũ Jul 25 '18 at 3:51

So you know how to get the value from a click, then pass it to a function that uses AJAX to send it to your PHP page.

In JavaScript


function passToPHP(myVariable){

    var dataString = 'var1=' + myVariable 

            type: "POST",
            url: "UpData.php",  //my PHP page
            data: dataString,
            cache: false,
        success: function() {
            console.log("Data sent by POST");


if (isset($_POST['var1'])) {
    $thePassedVariable = $_POST['var1'];
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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