I tried to run the code below but it doesn't work, and I tried everything I remember and couldn't get to work.
AJAX Call
var status = $(this).prop("checked");
var room_id = id;
$.post('maintenanceControl.php', {action: status, id: room_id});
PHP Script
<?php
if (isset($_POST['action']) && !empty($_POST['action']) && isset($_POST['id']) && !empty($_POST['id'])) {
$action = $_POST['action'];
$id = $_POST['id'];
if ($action) {
return manageMaintenance($id, true);
} else {
return manageMaintenance($id, false);
}
}
function manageMaintenance($room_id, $status)
{
$jsonString = file_get_contents('status.json');
$data = json_decode($jsonString, true);
foreach ($data['rooms'] as $key => $entry) {
if ($key == $room_id) {
$data[$key]['maintenance'] = $status;
}
}
$newJsonString = json_encode($data);
file_put_contents('status.json', $newJsonString);
return true;
}
At first I thought it was a malfunction but the example below worked just fine
$.post( "test.php", function( data ) {
alert(data);
});
PHP
<?php
echo "test";