1

I have seen this answer (and a couple others as well) and it provides an understandable way of accessing a PHP array. But in the example, the PHP and the JavaScript are in the same file.

However, I can not write Javascript in my PHP file for some reasons, and what I am rather doing is invoking the JS present in another file. So I need to access an array from the PHP in the JS file. How can I do that?

I prefer it without AJAX (if possible) because I have not yet started learning AJAX.

10
  • I am looking for a way to do it before I can try something. Commented Aug 9, 2014 at 23:16
  • @Boax Why did you remove my formatting? Commented Aug 9, 2014 at 23:16
  • 2
    I removed the formatting because bold text is considered over the top for a whole a paragraph. The way it was written, it was almost rude. I'm sure you didn't mean it to be, but that's what it usually means. Commented Aug 9, 2014 at 23:18
  • 1
    @Zarah I have no quarrel with you. If you wish to learn how to better your questions read the FAQ and learn by example from other people's questions. Your remarks make this personal when this is a completely technical matter. Commented Aug 9, 2014 at 23:34
  • 1
    (Aside: Zarah, my undiagnosed OCD makes me notice that a good deal of your questions feature whole paragraphs in bold, and bold/italic is often mixed together for double-emphasis. The paradox is that this usually ends up making text less readable, unfortunately! I'd expect there are style guides available on the web for this sort of thing, if you wanted an independent opinion about it; but it would remain my view that it's not ideal formatting, and that over time it will just create edit work. Remember that questions are for posterity and not just for the poster). Commented Aug 9, 2014 at 23:44

2 Answers 2

1

You could use a JSONP approach:

Make your PHP file output your data wrapped in a function call - something like this:

callback(
  ['your', 'data', 'here']
);

To do this with a PHP variable you can do this:

callback(<?php echo json_encode($data) ?>);

Then in your JavaScript file do this:

function callback(data) {
  alert( data.join(' ') ); // will alert 'your data here'
}

In your HTML file just include the above JS file and afterwards include another script tag that points to your php file:

<script src="/path/to/local.js"></script>
<script src="http://yourserver.com/path/to/server.php"></script>

The browser will grab the first file, and evaluate the function declaration. Then it will grab the second file, and see that it is a function invocation and immediately execute your function with the data as the argument.

Sign up to request clarification or add additional context in comments.

7 Comments

I've added in a suggested edit, hope that's OK. Minor quibble, isn't this just a JSON approach, rather than JSONP?
I understand the JS part. But I do not understand callback( ['your', 'data', 'here'] ); part. What is the [...] construct? What should I read up on to understand this?
Secondly I can not add those <script> tags in HTML because I can not add anything to <head> and I can not add any <script> tags anywhere. I can just use an API function to load a JS function from an external file.
The callback([...]) is just javascript that is outputted from your PHP script. You can add the script tag anywhere in your HTML if you want it doesn't have to be in the HEAD.
The [] syntax is just a JavaScript array literal.
|
1

A quick and dirty solution would be to make your JavaScript file a PHP file and pass all the variables you need when including the script:

<script type="text/javascript" language="javascript" src="./script.php?var=<?php print(json_encode($var)); ?>"></script>

In the JavaScript file you can use the PHP variables like so:

var a=JSON.parse(<?php print($_REQUEST['var']); ?>);

3 Comments

Minor note: isn't the JSON class part of jQuery, rather than just JavaScript?
As far as I know JSON is included by default in all modern major browsers.. anyway for cross browser support i would include the JSON class manually
Ha, you're quite right - I didn't know that! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.