dte49889 2012-06-25 09:41
浏览 61
已采纳

将PHP数组传递给Javascript

I have an Array like below in PHP

array(
    (int) 0 => array(
        'id' => '1',
        'name' => 'Mink7',
        'about' => 'Creative Design agency',
        'logo' => null,
        'lat' => '13.004870000000',
        'lng' => '77.576686084656'
    ),
    (int) 1 => array(
        'id' => '2',
        'name' => 'Bakasura',
        'about' => 'Live 2 Eat',
        'logo' => null,
        'lat' => '13.005643569006',
        'lng' => '77.577485382942'
    )
)

What have i done. I load the Function in the Header and have written the following in the body tag.

<script type="text/javascript">
$(document).ready(function(){
        var startups = <?php echo json_encode($startups); ?>;
        $.each(startups, function(key, val) {
            markMe(val.lat, val.lng, val.name, val.description, val.logo);
        });
});
</script>

I am getting the following error

markMe is not defined

markMe(val.lat, val.lng, val.name, val.description, val.logo);

Javascript File that is attached to HEAD.

// JavaScript Document
$(document).ready(function () {
    var map = new GMaps({
        div: '#map',
        lat: 13.00487,
        lng: 77.576729,
        zoom: 13,
    });

    /*
    GMaps.geolocate({
        success: function (position) {
            map.setCenter(position.coords.latitude, position.coords.longitude);
        },
        error: function (error) {
            alert('Geolocation failed: ' + error.message);
        },
        not_supported: function () {
            alert("Your browser does not support geolocation");
        },
        always: function () {
            //alert("Done!");
        }
    });
    */

    map.addControl({
        position: 'top_right',
        text: 'Geolocate',
        style: {
            margin: '5px',
            padding: '1px 6px',
            border: 'solid 1px #717B87',
            background: '#fff'
        },
        events: {
            click: function () {
                GMaps.geolocate({
                    success: function (position) {
                        map.setCenter(position.coords.latitude, position.coords.longitude);
                    },
                    error: function (error) {
                        alert('Geolocation failed: ' + error.message);
                    },
                    not_supported: function () {
                        alert("Your browser does not support geolocation");
                    }
                });
            }
        }
    });

    map.addMarker({
        lat: 13.00487,
        lng: 77.576729,
        title: 'Lima',
        icon: "http://i.imgur.com/3YJ8z.png",
        infoWindow: {
          content: '<p>HTML Content</p>'
        }
    });

    function markMe(lat, lng, name, description, logo){
        map.addMarker({
            lat: lat,
            lng: lng,
            title: name,
            icon: "http://i.imgur.com/3YJ8z.png",
            infoWindow: {
              content: description
            }
        });
    }

});
  • 写回答

2条回答 默认 最新

  • doupingzhi9674 2012-06-25 09:45
    关注

    look for json_encode in PHP, and use JSON.parse in JavaScript (or a library like jQuery.parseJSON if you need broad browser support)

    -- after update

    instead of function markMe(lat, lng, name, description, logo){

    do: markMe = function (lat, lng, name, description, logo){

    that's because a function declared in a function is not availlable outside that function

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料