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.

Community
  • 1
  • 1
Solace
  • 8,612
  • 22
  • 95
  • 183
  • I am looking for a way to do it before I can try something. – Solace Aug 09 '14 at 23:16
  • @Boax Why did you remove my formatting? – Solace Aug 09 '14 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. – Boaz Aug 09 '14 at 23:18
  • @Boaz I'm afraid I can't agree, I rather find your reason subjective. I use it so that the reader can quickly get to the part of the question which describes the question in one or two statements. The rest of it is context. There is no point of being rude or not rude when asking a question at SO. – Solace Aug 09 '14 at 23:23
  • This is not subjective. This is basic Internet etiquette. Not to mention the question is very short, so you end up marking most of it as bold. – Boaz Aug 09 '14 at 23:24
  • Hi Zarah. I agree with Boaz, so I've rolled back. Emboldening whole paragraphs is regarded nearly as the equivalent of shouting, and it isn't necessary to improve readability, especially in a relatively short piece. – halfer Aug 09 '14 at 23:28
  • @Boaz Oh thank you. Thank you for telling me that. If you didn't teach me, how would I learn the "internet" etiquette? – Solace Aug 09 '14 at 23:28
  • @halfer I will get back to this once I have solved the problem my question was about. – Solace Aug 09 '14 at 23:32
  • 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. – Boaz Aug 09 '14 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). – halfer Aug 09 '14 at 23:44

2 Answers2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
jhummel
  • 1,724
  • 14
  • 20
  • I've added in a suggested edit, hope that's OK. Minor quibble, isn't this just a JSON approach, rather than JSONP? – halfer Aug 09 '14 at 23:37
  • 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? – Solace Aug 10 '14 at 00:05
  • Secondly I can not add those ` – Solace Aug 10 '14 at 00:08
  • 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. – jhummel Aug 10 '14 at 00:41
  • The `[]` syntax is just a JavaScript array literal. – halfer Aug 10 '14 at 00:41
  • @halfer I think that would be JSONP - the callback wrapped around the JSON is the 'P' part. Correct? – jhummel Aug 10 '14 at 00:42
  • @jhummel: "JSONP" just makes me think there's cross-domain issues, when of course the JSON isn't going over HTTP at all. Fair enough though. – halfer Aug 10 '14 at 00:45
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']); ?>);
halfer
  • 19,824
  • 17
  • 99
  • 186
Rico Ocepek
  • 767
  • 6
  • 20
  • Minor note: isn't the `JSON` class part of jQuery, rather than just JavaScript? – halfer Aug 09 '14 at 23:52
  • 1
    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 – Rico Ocepek Aug 09 '14 at 23:54
  • Ha, [you're quite right](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FJSON) - I didn't know that! Thanks. – halfer Aug 09 '14 at 23:56