I'm trying to save some settings into my SQL Database and restore them using ajax and php. But the result is only one long JSON Value instead of 3. So i cant access "setting1" for example.
Settings: "{"setting1":"true","setting2":"false","setting3":"10"}"
this is my savemethod. the variables are all set.
let newSettings = {
"setting1": setting1_value,
"setting2": setting2_value,
"setting3": setting3_value
};
newSettings = JSON.stringify(newSettings);
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "save-settings.php");
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
xhttp.send(newSettings);
This is the php script to save the settings.
include "connection.php"
$newSettings = file_get_contents("php://input");
$object = json_decode($newSettings, true);
$sql = "UPDATE mytable SET Settings = '$newSettings'";
$result = $connect->query($sql);
And this method should read out the settings from SQL.
include "connection.php";
$sql = "SELECT Settings FROM Leads_Zugriff WHERE atpid = 21001";
$result = $connect->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
print(json_encode($row, true));
}
}
This calls the php script that reads out the settings
function readSettings() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
console.log(myObj);
}
};
xhttp.open("GET", "read-settings.php");
xhttp.send();
}