doujiao3016 2019-06-16 19:11
浏览 72
已采纳

读取时,我对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.

  • 写回答

1条回答 默认 最新

  • dongnai3960 2019-06-16 19:17
    关注

    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;
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?