You need a PHP forum for your PHP questions.
I don't use or know PHP. But I have often needed to pass some data into a page. So, I can offer some general suggestions.
1. It is bad practice to embed JS in the page. I always try to keep my JS in .js files. But sometimes it is necessary to put a little bit of JS in a page - for example, when you want to include some data that JS will use, and maybe it is inconvenient to have your server language make substitutions in the JS file.
2. I don't recommend embedding text (and hiding it?) or using hidden inputs. There's no good reason to do it (other than in some cases within forms that will later be submitted), and only complicates retrieving the values in your JS code.
3. When it is just a single value, I write a <script> block, and only set the variable. Following good practice, the JS code that reads the variable will be in some .js file.
Like I said, I don't know PHP. I use Ruby, and a template language called ERB. So, in ERB, I would write, simply:
- <script>
- var myJavascriptVariable = <%= my_ruby_variable %>;
- </script>
(Note if it is a string value, you will need to add the quotes around the template substitution.)
4. When it is multiple variables, or some complex data structure, I use a JSON library on the server to create a Javascript object declaration. This is very convenient, because JSON (Javascript Object Notation) was designed specifically to be usable as Javascript object declarations.
So, if I have some Ruby structure, say, a list of objects, I can quite simple create a javascript object declaration:
- <script>
- var myJavascriptObject = <%= my_ruby_object.to_json %>;
- </script>
JSON handles simple variables as well. And it will quote string variables. So, you don't have to worry about adding quotes yourself should your "object" be a simple string variable.
The HARD WAY to do this would be something like:
- <script>
- var myJavascriptObject = [
- {
- foo: <%= myRubyObject[0].foo,
- bar: <%= myRubyOjbject[0].bar
- },
- {
- foo: <%= myRubyObject[1].foo,
- bar: <%= myRubyObject[1].bar
- }
- // ... etc.
- ]
- </script>
Or, worse - making a zillion discrete variables.
I like doing things the easy way!
It may be worth your while to learn how to use JSON in PHP.
Another way, without writing a <script> block at all, is to use a data- attribute on some element, perhaps the <html> element or some other fairly top-level element.
jQuery makes it very easy to retrieve data set in a data- attribute, and... conveniently understands JSON, and will make a JS object if it finds JSON in a data- attribute.
So, you could write (again, in Ruby/ERB, because I do not know PHP...)
- <html data-myJavascriptObject='<%= my_ruby_object.to_json %>'>
Note you should use single-quotes, because the JSON might contain double quotes. The JSON library will deal with encoded quotes-within-quotes though.
Then, to read it in your JS code:
- var myJavascriptObject=$(html).data("myJavascriptObject");
If you are a purist about not mixing JS code into an HTML page, you can use data- attributes. I'm not that extreme a purist, so I use the first method. I do use data- attributes - and quite a bit - but only when I want to associate some data with specific HTML nodes in the page.