Copy php array to a javascript array

Posted on 2005-04-27
Medium Priority
Last Modified: 2008-03-04
Hope someone can help with this.

I have an array that I got from a database and want to load it into javascript array.  It seems I have tried everything I can think of and have tried to find another question that fits my situation.

Here is a small section of the code:

echo "<script type=\"text/javascript\" language=\"Javascript\">";

jimageNum[0]=<?php echo $imageNum[0]; ?>;

echo "</script>";

The 4th line is where I am trying to set the first element of the javascript array to the first element of the php array.  I will be replacing this with a for loop.  Currently this is in the head, but I have tried it in the body.

I'll be thankful for any help on this.

Question by:Jeiko
LVL 13

Accepted Solution

StormyWaters earned 800 total points
ID: 13881740
That should be fine the way it is... Since it isn't, can you post the View Source of the relevant bit of the page (not the PHP, the actual generated code)

One thing I would do differently is not echo the <script> tags, just put them in the HTML.

Expert Comment

ID: 13881784
jimageNum[0]=<?php echo $imageNum[0]; ?>;

Shouldn't that be:

jimageNum[0]=<?php $imageNum[0]; ?>;

Author Comment

ID: 13881847
StormyWaters, you taught me something very valuable.  I looked at the source code and the problem was obvious.  I was missing the quotes.  It should be:

jimageNum[0]="<?php echo $imageNum[0]; ?>";

Thanks a bunch!  Here's a few extra points.
LVL 10

Expert Comment

ID: 13889599

Jeiko are you all set with this question?  It sounds like you are.   I think you forgot to accept the answer


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question