I am trying to use a Dojo (ajax) table on my page that has detail rows in which I want to draw detail tables. The tables have a format event which calls formatDetail. In this event I would like to build a simple table using JSON data.

My problem is how do I use PHP to create the JSON data object so that my formatDetail javascript can use it?

My test code so far is, in the header:

function formatDetail(inWatchId, inRowIndex) {
	alert("Length is: " + myJSON.length);

And in the body of the page:

$stmt = sqlsrv_query($connection,$query);
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
	$arr[] = $row;
$objJSON = '{"sample":'.json_encode($arr).'}';
echo "<script>var myJSON = $objJSON;</script>";

When I try this, the js alert displays "Length is undefined"

What am I doing wrong?

7 Years
Discussion Span
Last Post by agrarian

Don't try to mix creating your own json strings

$objJSON = array('sample' => null);
// whatever
$objJSON['sample'] = $arr;
$objJSON = json_encode($objJSON);
echo '<script type="text/javascript">var myJSON = ' . $objJSON . ';</script>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.