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 8050501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:06:15+00:00 2026-06-05T07:06:15+00:00

How can I access a JSON array from a PHP variable in an external

  • 0

How can I access a JSON array from a PHP variable in an external JavaScript file without creating a global variable?

I have a PHP variable $customers that contains a json_encoded array:

$customers = [{"id":"12","name":"Abe Adams"},{"id":"17","name":"Bill Brown"}]

I want to use the contents of the array in a jQuery UI Autocomplete text input.

Rather than make an Ajax request for the autocomplete options, I’d prefer to use the values in the $customers array, which is passed in and ready to go when the user arrives at the page.

I know I can pass the contents of $customers to a JavaScript variable prior to loading the external JavaScript file:

<script>var customers = <?php echo $customers; ?></script>
<script src="external.js"></script>

But this requires creating a global JavaScript variable.

Is there a better way to access the $customers array from my external JS file?


This question has been asked several times previously:

Question: Access PHP var from external javascript file
Accepted Answer: Create global var

Question: Pass vars from php to js without ajax
Top Answer: Use Ajax

Question: php file send variable to .js external file
Top Answer: Use Ajax

Question: Reading a PHP file variables from external JS
Top Answer: Use eval

Question: Return JSON data from PHP, and use it using Javascript
Accepted Answer: Create global var

  • 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-06-05T07:06:15+00:00Added an answer on June 5, 2026 at 7:06 am

    You could also use a requirejs “pattern”:

    external.js:

    define([], {
        init: function (customerData) {
            ... all your setup
        }
    });
    

    On page:

    require(['external'], function (externalSetupFile) {
        var customers = <?php echo $customers?>;
        externalSetupFile.init(customers);
    });
    

    A big benefit to (in general) having some sort of “init” method that fires off the enhancement of the site is that you can do things like this. For example:

    Application = {
       init: function (bootstrapData) {
           UserModel.create(bootstrapData.userModelData); //for example 
           this.config = bootstrapData;
           this.setupUI();
       },
       setupUI: function () {
          //setup autocomplete and the like
          $("#someEl").autocomplete(this.config.customers);
       }
    }
    

    An even better part of this is that you can split up what you’re doing into small sections, which makes testing and debugging a heck of a lot easier.

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

Sidebar

Related Questions

I have some JSON data that I get from a server. In my JavaScript,
I'm sending the following from PHP to JSON print json_encode(array('success' => TRUE, 'status' =>
Can anyone please help me to understand how should i access json data in
Can someone see what I'm doing wrong with this php array? Im trying to
The following is encoded JSON data from a PHP webpage. { { news_date =
I can not work out the javascript syntax to access elements of the following,
My question: How can I parse a json array? I send it like this:
Okay I have a JSON file and it has a set of objects that
I am echoing JSON from a php server back to the browser. I am
I am receiving a JSON object from a PHP get request. It looks like

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.