i always get "Uncaught SyntaxError: Unexpected identifier" when i try to echo a PHP Variable to a JS Variable.
<script type="text/javascript">
$(function () {
var options = {
float: false,
removable: '.trash',
removeTimeout: 100,
acceptWidgets: '.grid-stack-item',
resizable: { handles: 'e, se, s, sw, w' }
};
$('#grid').gridstack(options);
var data = "<?php echo json_encode($serializedData); ?>";
My PHP:
$serializedData = array();
$str = "SELECT gri_id as 'id', gri_plugin as 'plugin', gri_gridContent as 'content', gri_gridPosX as 'x', gri_gridPosY as 'y', gri_gridSizeX as 'width', gri_gridSizeY as 'height' FROM tGrid WHERE gri_location = '$loc'";
$stmt = $db->prepare($str);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $value) {
$function = $value["plugin"];
$data = $function($value['id'], $db);
$value['content'] = $data;
array_push($serializedData, $value);
}
Assuming JSON is a valid snippet of javascript, why should we wrap it into quotes? You expect data will become Object, but it becomes String for now.
Do:
var data = <?php echo json_encode($serializedData) ?>;