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?

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>

Thanks! That enabled me to get it working!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.