How to automatically pass javascript variable after the session is done to the mysql database?

I have searched for ways and I can’t implement it the way I want it’s result on my part.

if(snakeX < box || snakeX > 17 * box || snakeY < 3*box || snakeY > 17*box || collision(newHead,snake)){
                    clearInterval(game);
                    dead.play();
                    alert("You died.");
                    window.location.href = "home.php";
                }

I have this part of a JavaScript code to a snake game and I wanted to log the username and the score of that user, I was able to log the ‘username’ but this is the part where i’m stuck, logging the ‘score’ as well.

This is my score.php page

<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "highscore";

$con = mysqli_connect($host, $username, $password, $database);

$query = "SELECT * FROM user";
$highscore = $con->query($query) or die($con->connect_error);
$row = $highscore->fetch_assoc();
$total = $highscore->num_rows;
?>

//part of the html code
<?php do { ?>
        <tr>
          <td><?php echo $row["username"]; ?></td>
          <td><?php echo $row["score"]; ?></td>
        </tr>
      <?php  } while($row = $highscore->fetch_assoc()); ?>

The output will be ‘username’ while the score is 0 because there is no value yet but when the game starts, it will be updated once the game is finished. If the player had 13 scores then that would be added to the database which will then be shown onto the score.php page.

I have tried passing variables but it seemed thats not possible since client based and server based thing. Most solutions I found were functions with the button but mine has none and i still tried to implement it on my terms which was not successful