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

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口