What is the safest way of passing arguments from server-side PHP to client-side JavaScript [duplicate]
My favorite way is : <?php $var = array( ‘prop1’ => ‘value1’, ‘prop2’ => ‘value2’, // … ); ?> <script type=”text/javascript”> var varNameSpace = <?php echo json_encode($var); ?>; alert( varNameSpace.prop1 ); // -> ‘value1’ </script> Using json_encode() ensures that the values passed to Javascript are escaped and well formatted. Using a common variable container also … Read more