1

I want to get data array from php to Javascript. (Actually I am doing plotting using javascript library and the data is in the database: I get the data using php script and want to use that data for plotting). I have tried to use a JSON for this. My code looks follows but it is not working. Please give me a help on this

 <script type="text/javascript">
<?php
 $php_arr=array('abc','def'); // I want to transport this array to javascript
 echo "display_diagram(" . json_encode($php_arr) . ")";
?>
 function display_diagram(data) {
 obj = JSON.parse(data); // this is not working for me 
asked Sep 8, 2013 at 16:42
2
  • what means not working? Commented Sep 8, 2013 at 16:47
  • 2
    You don't have to parse it at all. The parameter "data" will be the actual array. Commented Sep 8, 2013 at 16:47

1 Answer 1

1

Try use data variable in display_diagram function without JSON.parse. You now give data attribute in json format and this not require additional json parsing.

Check this:

<script>
<?php
 $php_arr=array('abc','def'); // I want to transport this array to javascript
 echo "display_diagram(" . json_encode($php_arr) . ")";
?>
function display_diagram(data){
 obj = data;
 alert(obj);
}
</script>
answered Sep 8, 2013 at 16:48
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.