2

I want to pass a php array to javascript. I've tried several examples taken form this site, but the rest of my code seem not to recognize them. I think the problem is in the quotes or format of the array.

First: var functionlist defined as below works OK.

<script type="text/javascript">
    var functionlist = Array('1','2','3','4','5','6','7','8','9','10','11','12');
    //Rest of the code
</script>

Second: var functionlist defined as below works OK.

<script type="text/javascript">
    var functionlist=Array("1","2","3","4","5","6","7","8","9","10","11","12");
    //Rest of the code
</script>

But the code below is not working, despite the fact that echoing $TransfArray renders something quite similar to the above.

<?php
    for ($i = 0; $i <= 12; $i++) {
        $OriginalArray[$i] = $i;
    }
    $TransfArray= "'" . implode("','", $OriginalArray) . "'";
?>

<script type="text/javascript">
    var functionlist = Array(<? echo $TransfArray; ?>);
    //Rest of the code
</script>

Nor does the code below

<?php
    for ($i = 0; $i <= 12; $i++) {
        $OriginalArray[$i] = $i;
    }
    $Original_to_json = json_encode($OriginalArray);
?>

<script type="text/javascript">
    var functionlist =  <?php echo $Original_to_json; ?>;
    //Rest of the code
</script>

Does anyone detect the problem? Thnaks in advance.

5
  • How does your original php array look like and how would you like it to look in javascript? json_encode really should do the trick nicely. Commented Feb 26, 2013 at 13:20
  • What does the second example output to the browser? Commented Feb 26, 2013 at 13:22
  • What does the output of those PHP examples give? i.e. How does it differ to what works? Ideally, you want the output to be: var functionlist = ['1','2','3','4','5','6','7','8','9','10','11','12']; and without the quotes if you want to deal with numbers in JavaScript. Commented Feb 26, 2013 at 13:22
  • Since i'm rather newbie, i'm just trying to run the code above. But can't pass $OriginalArray defined in php (numbers from 1 to 12) to var functionlist. Commented Feb 26, 2013 at 13:23
  • Show us the Javascript that is using the array. The array gets created properly, so your Javascript must be at fault. Commented Feb 26, 2013 at 13:34

3 Answers 3

2

Use casting as follows,

<?php
for ($i = 0; $i <= 12; $i++) {
$OriginalArray[]= (String)$i;
}
$Original_to_json=json_encode($OriginalArray);
?>

<script type="text/javascript">
var functionlist =  <?php echo $Original_to_json; ?>;
//Rest of the code
</script>
4
  • Why? He shows no code that requires the values of the Javascript array to be a string rather than an int. Commented Feb 26, 2013 at 13:27
  • @Crush user1726522 is almost reached his goal, i guess he may need some text as an identifier. Commented Feb 26, 2013 at 13:35
  • He might be treating it as a String, but Javascript should automatically transform it into a String in almost any instance he is using it. Unfortunately, we have no idea what the OP is doing with the array. Commented Feb 26, 2013 at 13:42
  • Wow! So many responses in minutes! Thanks to all of you guys. The rest of the code is rather complicated, and I don't understand it completely. Is a modified version to autocomplete a combobox taken from somacon.com/p241.php. In that site, the list is "manually" defined, whereas I wanted to load from a database. Adding a (String) as suggested by hilarudeens when defining $OriginalArray do the trick. Again, thanks a lot! PS: I should have asked before. I was searching for the problem for one and a half days. You solved it in minutes. Commented Feb 26, 2013 at 14:10
0

Writing JavaScript code with PHP is a terrible practice. You shouldn't be doing that. Instead, you can do something different and easy.

  1. In PHP, you generate a JSON document by converting the array to a JSON object. Use the json_encode function for that. Name your file "json.php" (though naming is data.json is probably a better practice, but you'll have to go through more steps).
  2. Now, you can include the file with in your HTML document <script src="json.php"></script> Or you can use AJAX to load it dynamically.
  3. The JSON object will have the JavaScript array.
2
  • @crush My suggestion is not about using json_encode or another function. It's about "not mixing" PHP with JavaScript. You should keep the front-end and back-end separate. Commented Feb 26, 2013 at 13:27
  • Then this should be a comment, not an answer. Commented Feb 26, 2013 at 13:28
-1

Try this :

<script type="text/javascript">
var functionlist =  <?php echo json_encode($OriginalArray); ?>;
//Rest of the code
</script>

Enjoy ;)

1
  • What did you do differently? Nothing. Commented Feb 26, 2013 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.