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 为什么eprime输出的数据会有缺失?
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题