Вам необходимо использовать AJAX.
Пример:
xhttp.open("POST", "path/to/post/handler.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("fname=Henry&lname=Ford");
В вашем случае:
if (distance < 0) {
clearInterval(x);
document.getElementById(elementId).innerHTML = "Expired";
//this is where you update the database:
xhttp.open("POST", "path/to/post/handler.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//use the xhttp object to send(POST) values to the database.
xhttp.send("message=Expired&id=242");
}
Подробнее здесь
Что вы используете в бэкэнде?Вам нужно будет обработать JavaScript POST:
Пример PHP будет:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $this->mysqli->prepare("UPDATE yourTable SET status=? WHERE id=?");
$stmt->bind_param('si', $_POST['message'], $_POST['id']);
$stmt->execute();
return $stmt->affected_rows;
$conn->close();
?>
Дайте мне знать в комментариях, если это работает ...