-1

I use Leaflet to show map, on onEachFeature function, I have inserted ajax to get variable to pass to PHP, like this

function onEachFeature(feature, layer) {
  layer.bindPopup(feature.properties.IDLo); 
  layer.on("click", function(e)
  {
    var popup = e.target.getPopup();
    var content = popup.getContent();
    $.ajax({
      url: "index.php",
      type: "GET",
      data: "content=" + content,
      success: function(data)
      {
        console.log(content);
      }
    });
  });   
};

And this is PHP code

<body>
        <h2>My Map</h2>
        <div>
            <?php 
            if(isset($_GET['content']))
                {
                    $uid = $_GET['content'];
                }
            else $uid = 0;
            ?>
        </div>
        <div><?php echo $uid;?></div>
        <div id="map"></div>
        <script src="js/new.js"> </script>
    </body>

I always get 0 result in PHP but on console in chrome, it's the content value. Is there any sugguest? Thanks in advance.

Huệ Vũ
  • 35
  • 7
  • Is `js/new.js` the javascript shown? If so it's not going to update the dom which has already been sent. Possible dupe: [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Lawrence Cherone Jul 21 '18 at 08:39
  • I think both the JavaScript function and the PHP codes are in the same page index.php . If I'm correct then put on some more codes here to check . – PHP Web Jul 21 '18 at 08:41
  • `js/new.js` is the file contains onEachFeature functions, not in the same page index.php. – Huệ Vũ Jul 21 '18 at 09:13

2 Answers2

0

Why not use the post method?

JS:

function onEachFeature(feature, layer) {
  layer.bindPopup(feature.properties.IDLo); 
  layer.on("click", function(e)
  {
    var popup = e.target.getPopup();
    var content = popup.getContent();
    $.post("index.php",{content},function(data) {
       console.log(data);
    });

  });   
};

HTML/PHP:

<body>
        <h2>My Map</h2>
        <div>
            <?php 
            if(isset($_POST['content']))
                {
                    $uid = $_POST['content'];
                }
            else $uid = 0;
            ?>
        </div>
        <div><?php echo $uid;?></div>
        <div id="map"></div>
        <script src="js/new.js"> </script>
    </body>

DEMO PHP :

<?php 
if(isset($_POST['content'])) {
   echo 'I came here via PHP, the value of content variable is: -> ';
   echo $_POST['content'];
}

?>
saibbyweb
  • 2,864
  • 2
  • 27
  • 48
  • No no no, I want `content` value to PHP, not versa, I want to get variable from js to PHP – Huệ Vũ Jul 21 '18 at 09:17
  • When `onEachFeature(feature, layer)` will be called, it will sent the value of `content` to PHP – saibbyweb Jul 21 '18 at 09:19
  • but how to get it on php? I'm a beginner on PHP, JS – Huệ Vũ Jul 21 '18 at 09:27
  • I made an edit to my answer. Copy the `JS` function and `DEMO PHP` from my answer to your files and run it. Analyze `DEMO PHP` and compare it with your browser's console to better understand what is happening. – saibbyweb Jul 21 '18 at 09:35
0

I think you're using the data field incorrect and it will just send the whole string.

Better use an object or array like this:

data: {content: content }

See the jquery documentation under data for further inspection: http://api.jquery.com/jQuery.ajax/

EDIT: Just adding that if you want to use the "=" syntax you can do it directly. url: "index.php?content=" + content
However using the data field is probably preferred and considered a better solution.

Johnstedt
  • 47
  • 6