如何在PHP中获取JavaScript变量值

时间:2018-11-13 05:34:10

标签: javascript php

如何在php中获取“变量滑块”值,然后在查询中将其用于“ WHERE”值?谢谢

  var slider = document.getElementById("myRange");
  var output = document.getElementById("time");
  output.innerHTML = slider.value;

  slider.oninput = function() {
    output.innerHTML = this.value;

    <?php
      $sql = "SELECT * FROM populasi INNER JOIN waktu ON populasi.idWaktu = waktu.id WHERE waktu.jam = ???? ";
      $result = $con->query($sql);
    ?>
  }

1 个答案:

答案 0 :(得分:1)

您无法像上面的代码那样获得javascript数据,但是您可以使用此代码, 您可以使用jquery然后使用ajax

$.ajax({
url: 'your url to receive data or whatever',
type: 'POST / GET depend on your need',
dataType: ' can be json, text , or something else',
data:some data you wanna send to,
success:function(data){
   console.log(data);
},
error: function(xhr, ajaxOptions, thrownError){
    console.log(thrownError);
}

示例:

var slider=$('#myRange');
slider.on('input',function(){
  $.ajax({
  url: 'http://example.com/intime', // Your url for receive data
  type: 'POST',
  dataType: 'text',
  data:{slide:slider.val()},// You can send multiple data like {test:test1,test2:test2, ...}
  success:function(data){
    console.log(data);// you can change this for your own function or whatever
  },
  error: function(xhr, ajaxOptions, thrownError){
    console.log(thrownError); // If error thrown here
  }
});

在服务器端:

$slider=$_POST['slide']; //Get slide from ajax
echo $slider; //you can use this var for proccessing or get query. etc.

如果此答案解决了您的问题,请标记为接受的答案。