将php变量传递给php函数内的javascript,即echos js代码

时间:2014-06-27 08:37:22

标签: javascript php wordpress

我正在编写WP插件,我正在使用php {{1}将JavaScript脚本写入页面文档的head }。我需要将一些echo变量传递给脚本。我试过了php但是这里没有传递正确的东西是我的功能:

json.encode()

当我打开页面上的WP function test_action() { global $wpdb; $contents = $wpdb->get_results("SELECT content FROM wp_map_user_demo"); $lats = $wpdb->get_results("SELECT latitude FROM wp_map_user_demo"); $longs = $wpdb->get_results("SELECT longitude FROM wp_map_user_demo"); //Modifying the header file of WP echo '<title>Demo</title> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=myKey"></script>'; //Including the custom Google Maps script echo "<script type='text/javascript'> var map; var contents = ". json_encode($contents) ."; var lats = ". json_encode($lats) . "; var longs = " . json_encode($longs). "; document.write('<p>' + lats[0] + '</p>'); function initialize() { var mapOptions = { center: new google.maps.LatLng(lats[0], longs[0]), zoom: 8 }; var map = new google.maps.Map(document.getElementById('map'), mapOptions); } //map.setOptions({draggable: true}); google.maps.event.addDomListener(window, 'load', initialize); </script>"; } 输出document.write('<p>' + lats[0] + '</p>');而不是纬度数据。我检查了php变量并且它们是正确的,所以我想我的问题是{{1用法。

感谢您的帮助...

3 个答案:

答案 0 :(得分:2)

您的JavaScript变量lats是一个对象数组,因此当您执行lats[0]时,您将获得数组中的第一个对象。要获取纬度值,您需要访问对象的latitude属性:

document.write('<p>' + lats[0].latitude + '</p>');

你的经度也一样:

longs[0].longitude

答案 1 :(得分:1)

我猜$wpdb->get_results不会返回String而是Result Set对象。如果您想直接访问它,请尝试使用$wpdb->get_var

编辑:或者像MrCode一样访问Result Set的属性。

答案 2 :(得分:1)

试试这个;)

function test_action() {
    global $wpdb;
    $results = $wpdb->get_results("SELECT content,latitude,longitude FROM wp_map_user_demo");

    $contents = $lats = $longs = array();

    foreach($results as $result) {
        $contents[] = $result->content;
        $lats[] = $result->latitude;
        $longs[] = $result->longitude;
    }

    //Modifying the header file of WP
    echo '<title>Demo</title>
            <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
            <meta charset="utf-8">
            <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=myKey"></script>';
    //Including the custom Google Maps script
    echo "<script type='text/javascript'>
                    var map;
                    var contents = " . json_encode($contents) . ";
                    var lats = " . json_encode($lats) . ";
                    var longs = " . json_encode($longs) . ";

                    document.write('<p>' + lats[0] + '</p>');
                    function initialize() {
                    var mapOptions = {
                      center: new google.maps.LatLng(lats[0], longs[0]),
                      zoom: 8
                    };
                    var map = new google.maps.Map(document.getElementById('map'),
                        mapOptions);
                  }
                  //map.setOptions({draggable: true});
                  google.maps.event.addDomListener(window, 'load', initialize);
            </script>";
}