I am using the Gridstack library to create a dashboard. The widgets x,y,width and height can be put into a json, I am trying to save this json into MySQL.
I got the json array to enter a table in MySQL by pressing a button to work, when I was at my unviversity. When I went home, it stopped working. I am using PHP and MySQL.
The first issue was that the line below stopped working (it was fine before I went home, didn't touch code).
$data = $_GET['name'];
Had to be changed to this:
$data = isset($_GET['name']);
No idea why. Also the rest of the PHP stopped working. No errors, just does nothing. It isn't the script I have a problem with. All the Javascript work just fine.
Rest of the code:
$('#save').click(function(){
var res = _.map($('.grid-stack .grid-stack-item:visible'), function (el) {
el = $(el);
var node = el.data('_gridstack_node');
return {
id: el.attr('data-custom-id'),
x: node.x,
y: node.y,
width: node.width,
height: node.height
};
window.location.href = "index.php?string=" + JSON.stringify(res);
});
<?php
$connect = mysqli_connect("localhost", "root", "", "widgetCollection");
$data = isset($_GET['string']);
//$data = $_POST['variable'];
$array = json_decode($data, true);
foreach((array)$array as $row) {
$sql = "INSERT INTO grids(x, y, width, height) VALUES('".$row["x"]."', '".$row["y"]."', '".$row["width"]."', '".$row["height"]."');";
mysqli_query($connect, $sql);
}
?>
alert(JSON.stringify(res));
});