读取时,我对PHP的jQuery变量为空

I have an HTML slider called myRange that represents years. I want to get the value and print it in this field when I click a button:

<div id="test">HELLO</div>

I read the value in the script:

var slider = document.getElementById("myRange");

function post()
{
  var year = slider.value
  $.post('year.php', {slideryear:year}, 
  function(data){
    $("#test").html(data);
  });
}

And this is the year.php file:

<?php
    $year = $_POST['slideryear'];
?>

When I click the button to show the number in the id="test" paragraph, the paragraph goes blank ("HELLO" disappears). My first attempt with PHP, what am I doing wrong?

I'm running on Apache.

You only store the post value in the variable $year but you have to give the value back as the response if you want to get it in the callback or else HELLO will be replaced with an empty string.

Try for example:

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $year = $_POST['slideryear'];
    echo $year;
}

?>