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
Malintha
4,80812 gold badges56 silver badges87 bronze badges
-
what means not working?hek2mgl– hek2mgl2013年09月08日 16:47:51 +00:00Commented Sep 8, 2013 at 16:47
-
2You don't have to parse it at all. The parameter "data" will be the actual array.Pointy– Pointy2013年09月08日 16:47:56 +00:00Commented Sep 8, 2013 at 16:47
1 Answer 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
Mateusz Mania
8494 silver badges15 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default