Skip to content Skip to sidebar Skip to footer

How To Insert A Php Variable Into A Javascript Object/array

Possible Duplicate: What is the safest way of passing arguments from server-side PHP to client-size JavaScript Goal: Replace the static file name in the javascript object with p

Solution 1:

Why you are not using json_encode

<?php$data = array( );
 foreach (glob($queryglob) as$filename) $data[$filename] = array( 'caption' => '' );
?><scripttype="text/javascript">var data = <?phpecho json_encode( $data ); ?></script>

That is much more clever than building a string

Post a Comment for "How To Insert A Php Variable Into A Javascript Object/array"