Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm coding a plug-in for WP and I'm writing a JavaScript script to the head of the page document using php's echo. I need to pass some php variables to the script. I tried json.encode() but it does not pass the right thing here is my function:

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

When I open the page on WP document.write('<p>' + lats[0] + '</p>'); outputs [object Object] on the page instead of latitude data.I checked the php variables and they are correct, so I guess that my problem is with JavaScript usage.

Thanks for your helps...

share|improve this question
    
Try this: document.write('<p>' + json_stringify(lats) + '</p>'); as your variable is json encoded and let us know what is output. – Code Lღver Jun 27 '14 at 8:41
up vote 1 down vote accepted

Try this ;)

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>";
}
share|improve this answer

Your JavaScript variable lats is an array of objects, so when you do lats[0] you're getting the first object in the array. To get the latitude value you need to access the latitude property of the object:

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

It's the same for your longitude:

longs[0].longitude
share|improve this answer
    
Thank you so much I didn't know this info :) – dogadikbayir Jun 27 '14 at 8:58

I guess $wpdb->get_results doesn't return a String but an Result Set Object. You should try $wpdb->get_var if you want to access it directly instead.

Edit: Or access the properties of the Result Set like MrCode said.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.