-1

Possible Duplicate:
Php pass array to Javascript
How to pass PHP array parameter to Javascript Function?

So I'm trying to pull an individual element at random from an array consisting of data pulled from a database. This is a simplified version of the php code.

        function getID() {
                $result = array('1','2','3','4');

                $id = $result[array_rand($result)];
                return $id;
        }

From here, I'm defining a JavaScript variable using the returned ID.

Here's the weird thing, when I define the JS variable using the passed return $id, the javascript variable is defined as = "Array" instead of the $id data.

IF I change the php code to this:

        function getID() {
                $result = array('1','2','3','4');

                $id = array_rand($result);
                return $id;
        }

The javascript variable is defined as the $id, which is the array key, not the array data.

So I tried changing the php code to this:

        function getID() {
                $result = array('1','2','3','4');

                return $result[0];
        }

But the javascript gets defined as "Array" again.

It's as if the php won't take an array marker.

Any ideas here are welcome.

Community
  • 1
  • 1
Jmichelsen
  • 257
  • 5
  • 19
  • 1
    Have you tried converting the data to JSON with json_encode? – Matthew Blancarte Nov 07 '12 at 07:14
  • 2
    Show us html that it generates and how you print it out. – Vyktor Nov 07 '12 at 07:15
  • 1
    I nom'd this for re-opening because it is nothing close to a duplicate of the above threads. This is about javascript recognizing a specific piece of data from an array; not an entire array. – Daedalus Nov 07 '12 at 21:41
  • @Jmichelsen I'm unable to reproduce your error with the code provided; could you please post your html, javascript, and the rest of your php? – Daedalus Nov 07 '12 at 21:50
  • @Daedalus I got it working with the answer posted by PLB below. The entire PHP/JS is pretty complex, pulling data from MySQL and running other functions on it, so I wanted to keep things simple thus the code above. – Jmichelsen Nov 08 '12 at 00:14

1 Answers1

2

You can't directly pass arrays directly from php to javascript. Use json_encode to return your array as Json encoded string.

function getID() {
   $result = array('1','2','3','4');

   json_encode($result);
}

And in javascript you can do:

var obj = JSON.parse(jsonFormattedStringThatWasGeneratedByFunctionGetID);

Most browsers support JSON.parse()

Edit: I've misunderstood your question at first but you can still use this method and get desired item on client-side but this is far from perfect solution. I'll leave this answer as another option.

Leri
  • 12,367
  • 7
  • 43
  • 60
  • 3
    The user isn't trying to pass an array. They're trying to pass a single value, as evidenced by their question. – Daedalus Nov 07 '12 at 07:22
  • @Daedalus Thank you very much. I've edited answer hoping it maybe useful for OP. – Leri Nov 07 '12 at 07:30
  • @Daedalus That's right, just trying to get a single digit. However, PLB pointed me in the right direction. Did you have a different solution? Either way, appreciate the help. – Jmichelsen Nov 07 '12 at 08:04
  • @PLB That did the trick, sort of. Using json_encode I was able to get a JSON element consisting of {'title':'id'}. I used JSON.stringify to create a string, and result.split to get the ID I needed. Then I had to strip away the } from the JSON element. But in the end, success! Thanks a ton :) – Jmichelsen Nov 07 '12 at 08:30