Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I have a php array that I want to pass as a parameter to a javascript function onClick. The function, however, is in another file. I try to explain.

File map.php:

  <head>
     <script type="text/javascript" src="../public/js/map.js"></script>   
     <script type='text/javascript'>
         var js_array = [<?php echo '"'.implode('","', $php_array).'"' ?>];
         //alert(js_array[0]); //this alert works
     </script>     
  </head>
  //code...
  <form method="post" enctype="multipart/form-data" action="">
     <input type="submit" name="submit" value="Post" onClick="createMap(js_array); return false;"/>
  </form>

File map.js. I tried to print the array via an alert but does nothing:

  for(i=0; i<js_array.length; i++) {
     alert(js_array[i]);
  }

  function createMap(js_array) {
     //code...
  }

What's wrong? Thank you very much


EDIT Thanks for the replies but still does not work. I modified the code like this:

File map.php:

  <head>
     <script type='text/javascript'>
         var js_array = JSON.parse(<?php echo "json_encode($addresses)"; ?>);
         //alert(js_array[0]); //this alert works
     </script>     
     <script type="text/javascript" src="../public/js/map.js"></script>
  </head>
  //code...
  <form method="post" enctype="multipart/form-data" action="">
     <input type="submit" name="submit" value="Post" onClick="createMap(js_array); return false;"/>
  </form>

File map.js:

  for(i = 0; i < js_array.length; i++) {
     alert(js_array[i]);
  }

  function createMap(js_array) {
     //...
  }
share|improve this question

marked as duplicate by Daedalus, Jonathan Kuhn, DanFromGermany, Josh Mein, Dan S Jan 16 '14 at 22:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Almost a word for word duplicate, how could you not see this? –  Arian Faurtosh Jan 16 '14 at 21:39
1  
Don't quote json_encode($addresses). That's a legit PHP function call, not something you want to just echo out. –  citizenslave Jan 16 '14 at 22:04
    
@citizenslave It's the same. Still not working.. –  user245679 Jan 16 '14 at 22:06
    
To add to what citizenslave just said, by enclosing the call to json_encode in quotes, you are echo'ing out the literal string "json_encode(...)", not the return value from the function. That line should just be var js_array = <?php echo json_encode($addresses); ?>;. If that doesn't work, you would need to check what js errors are being reported. It might not even be this. –  Jonathan Kuhn Jan 16 '14 at 22:07
    
Does anything alert? What happens if you set js_array to a string array explicitly? What do you see if you move the PHP code into your form block so it just displays on screen? Can you alert the hard coded string array? Does the PHP output validate at JSONlint? –  citizenslave Jan 16 '14 at 22:08

1 Answer 1

I think you're having an issue because your map.js file is being loaded and executed before the script block that initializes js_array. Try switching your script blocks around.

Definitely use the json_encode method on the PHP side. This it what it was designed for.

After looking more carefully at your code, I see all of the " marks that you're including and I think it winds up being valid JSON, but your code will be easier to maintain and for others to help debug if you just use json_encode and JSON.parse.

Your ACTUAL problem though is just the order of the script blocks.

I tested this code, it works.

<head>
  <?php $addresses = array("test","test"); ?>
  <script type='text/javascript'>
    var js_array = <?php echo json_encode($addresses); ?>;
    //alert(js_array[0]); //this alert works
  </script>     
  <script type="text/javascript" src="map.js"></script>
</head>

You also forgot the ; in your original code...

share|improve this answer
1  
This answer would be better with examples. As a note though, I'm not the one that voted down. –  Daedalus Jan 16 '14 at 21:48
    
Why would the order matter? If map.js is defining a function, it shouldn't matter if the array that is passed in as a parameter is defined before or after the function. –  Jonathan Kuhn Jan 16 '14 at 21:49
1  
@JonathanKuhn .. Because the map.js file has a for loop outside of a function. –  Daedalus Jan 16 '14 at 21:50
1  
You wrapped the json_encode call in quotes. Don't. –  citizenslave Jan 16 '14 at 22:05
1  
Thank you, now it works :) –  user245679 Jan 17 '14 at 7:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.