doupeng3501 2018-07-21 08:29
浏览 32

将变量从ajax传输到PHP

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.

  • 写回答

2条回答 默认 最新

  • drrkgbm6851 2018-07-21 09:00
    关注

    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'];
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度