Values from database converted to JS shows as [object Object]
-
I’m trying to get the data from the database to JavaScript but I’m getting
[object Object] in the console.Here is my code:
global $wpdb; $retrieve_data = $wpdb->get_row( "SELECT lat,lng FROM markers" ); function object_to_array($retrieve_data) { if(is_array($retrieve_data) || is_object($retrieve_data)) { $result = array(); foreach($retrieve_data as $key => $value) { $result[$key] =object_to_array($value); } return $result; } return $retrieve_data; } $val=object_to_array($retrieve_data); <script type="text/javascript"> var myJsarray = <?php echo json_encode($val); ?>; console.log('data from database '+myJsarray); </script>
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
The topic ‘Values from database converted to JS shows as [object Object]’ is closed to new replies.