在返回js代码的php函数中传递一个php变量给javascript


Passing a php variable to javascript inside php function that echos js code

我正在为WP编写插件,我正在使用php的echo编写JavaScript脚本到页面文档的head。我需要将一些php变量传递给脚本。我尝试了json.encode(),但它没有传递正确的东西,这里是我的函数:

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>";
}

当我在WP上打开页面时,document.write('<p>' + lats[0] + '</p>');在页面上输出[object Object]而不是纬度数据。我检查了php变量,它们是正确的,所以我想我的问题是与JavaScript的使用。

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

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

对于经度也是一样:

longs[0].longitude

试试;)

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>";
}

我猜$wpdb->get_results不返回String,而是Result Set对象。如果你想直接访问它,你应该试试$wpdb->get_var

编辑:或者像MrCode说的那样访问Result Set的属性