POST method not sending data from JavaScript to PHP file

I have some data stored in local storage that I want to send to a database. This is the JS code:

function sendData(){
          let input = 'new input';
          localStorage.setItem('spins', input);
          // Get user_id from session or wherever it's stored
          var user_id = <?php echo isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 'null'; ?>;

          if (user_id) {
            var value1 = localStorage.getItem('data');
            var value2 = localStorage.getItem('spins');

            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'update_values_to_database.php', true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.onreadystatechange = function() {
              if (xhr.readyState === 4 && xhr.status === 200) {
                try {
                  var data = JSON.parse(xhr.responseText);
                  if (data.success) {
                      localStorage.setItem('data', data.data);
                      localStorage.setItem('spins', data.spins);
                      
                  } else {
                      console.error('Failed to retrieve values from the database.');
                  }
                } catch (error) {
                  // Code to handle the error
                  console.error('An error occurred:', error.message);
                  window.location.href = "update_values_to_database.php";
                }     
              }
              };
          }
            xhr.send(JSON.stringify({ user_id: user_id, value1: value1, value2: value2 }));
          }

`

And this it the PHP

<?php
include("database.php");

// Retrieve data from the POST request
var_dump($_REQUEST);
$user_id = $_POST['user_id'];
$value1 = $_POST['value1'];
$value2 = $_POST['value2'];

// Update values in the database for the given user_id
// Modify this query according to your database schema
$query = "UPDATE users SET data = ?, spins = ? WHERE user_id = ?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, 'ssi', $value1, $value2, $user_id);
if (mysqli_stmt_execute($stmt)) {
    echo json_encode(array('success' => true));
} else {
    echo json_encode(array('success' => false));
}
?>

Both the file containing the JS code and that containing the PHP code are in the same directory. I don’t know what I’m doing wrong. Could someone tell me what the error could be?
Thanks in advance.

I’ve tried logging the error and it’s saying that the ‘user_id’, ‘value1’ and ‘value2’ keys are undefined and when I var_dump the $_REQUEST it returns an empty array. When I’ve made sure that all the variables being sent to the PHP file are being defined correctly.