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条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,