Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6110585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:31:57+00:00 2026-05-23T14:31:57+00:00

So i have a page called region.php, which in the header, loads JQuery, the

  • 0

So i have a page called region.php, which in the header, loads JQuery, the google maps API, and the custom Jquery functions I use for my app in a file called functions.js.

In region.php, I pull a few rows from the MySQL DB and store them in a php array, $regions.

In my functions.js file, I’m trying to json_encode the $regions array, and pass it to my function, to plays all of the lats and longitudes of the $regions array on the google map. However, IM having trouble getting the Php array into Javascript.

I had been following this but it doesnt seem to be working for me: Iterating through a PHP array in jQuery?. Doesn’t seem like the javascript can work with the php in the example they give

any ideas? (and I suppose if im going about this all wrong — what is the best way to get the php array into javascript?

Region.php
$regions = get_regions();

foreach($regions as $region) : 

    print $region['name']; 

endforeach;


print "<div id='map_view_canvas' style=\"width:300px; height:300px; \"></div>";

functions.js

$(document).ready(function() { 

    initialize_view_map(); 
}

function initialize_view_map() 
{   
    var latlng = new google.maps.LatLng(9.3939, 20.57268);
    var myOptions = {
        zoom: 2,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_view_canvas"), myOptions);

    var mapPoints = <?php echo json_encode($regions) ?>;

    $.each(mapPoints, function (i, elem) {

        var newLatLng = new google.maps.LatLng(elem.latitude, elem.longitude);

        var marker = new google.maps.Marker({
            position: newLatLng, 
            map: map, 
            draggable:false,
            animation: google.maps.Animation.DROP
        });

    });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:31:58+00:00Added an answer on May 23, 2026 at 2:31 pm

    From the example you link to:

    var arrayFromPHP = <?php echo json_encode($viewFields) ?>;
    

    that needs to go in a php file not your functions.js file, which is what it sounds like you’ve done. The code between is php, so needs to run on the server side, it can’t run in javascript (which is client side and so knows nothing about php or any server side code\variables\etc)

    EDIT:

    Now that you’ve added your code, try something like the following. Sorry, I’m not too familar with php, but hopefully you get the idea:

    Regions.php

    $regions = get_regions();
    
    foreach($regions as $region) : 
    
        print $region['name']; 
    
    endforeach;
    
    print "<script>";
    print "var mapPoints = ";
    echo json_encode($regions->toArray());
    print "</script>";
    print "<div id='map_view_canvas' style=\"width:300px; height:300px; \"></div>";
    

    Then your Javascript becomes (note the mapsPoint variable has been output by the regions.php already so I remove it here)

    $(document).ready(function() { 
    
        initialize_view_map(); 
    }
    
    function initialize_view_map() 
    {   
        var latlng = new google.maps.LatLng(9.3939, 20.57268);
        var myOptions = {
            zoom: 2,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_view_canvas"), myOptions);
    
        $.each(mapPoints, function (i, elem) {
    
            var newLatLng = new google.maps.LatLng(elem.latitude, elem.longitude);
    
            var marker = new google.maps.Marker({
                position: newLatLng, 
                map: map, 
                draggable:false,
                animation: google.maps.Animation.DROP
            });
    
        });
    }
    

    EDIT2:

    Another thing to try is rename your functions.js to function.js.php, update any references to it and then try that – then it would hopefully run through the php pipeline and run the php code in your js file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page called send.email.php which sends an email - pretty simple stuff
I have a page called send.email.php which sends an email - pretty simple stuff
I have a page called list.php which retrieves from database and shows all the
I have a page called index.php which is calling a function writelog in includes/Logger.php
So I have a page called latest news and using a custom template t_latest_news.php
I have a page called Error.php. Variables are usually passed to it using the
Let's say I have a page called display.php and the user is viewing display.php?page=3
My project is about a school admin I have a page called : createClass.php,
I have a page index.php where i have a link called add_users.php. In add_users.php,
I have this page called up.php that adds 1 to a txt file set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.