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 
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>

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.