Sending Findallbysql Results To Javascript

Hello everybody,

I am trying to send data to javascript:

In the controller I have (I took out the sql):

$data = Entries::model()->findallbysql();

In the view I am calling a function with the data:

<script type="text/javascript">functionxy(<?php echo CJavaScript::jsonEncode($data); ?>)</script> 

in the js function i am trying to alert the recieved data:


results into undefined.

However using this directly in the view works:

<?php echo $data[0]->comment; ?>

But I need the data in javascript.

What am i doing wrong?

How am I supposed to send data to js correctly?

Any help is greatly appreciated

Although you may be echoing the data to the browser you’re not assigning it to any variable. Javascript doesn’t automatically know about a PHP variable known as $data.

Try this:

<script type="text/javascript">var data = <?php echo CJavaScript::jsonEncode($data); ?></script>

And then use the variable data.