Copy php array to a javascript array

Posted on 2005-04-27
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

    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.
    LVL 8

    Expert Comment

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

    Shouldn't that be:

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

    Author Comment

    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


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


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Both jQuery and Mootools offer some nice, easy to use scripts. Mootools is best known for their interactive slideshows and jQuery for their menus, but both have many other uses as well.   jQuery is basically a toolkit with many DOM (http://en.wik…
    This article shows how to read a single webpage's HTML into a string variable, and it also shows how to automate a sequence so you can read and process a list of webpages.  I was tasked with reading and parsing a series of webpages to collect some s…
    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…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now