dqjl0906 2019-05-01 08:33
浏览 101
已采纳

如何在HTML中添加JSON AJAX请求中的滑块

I've got 2 tables that are storing movie ID, posters and run-times from SKYTV and NOWTV. They hold and ID number and a poster path. When the NOWTV checkbox is clicked, NOWTV movies show. When SkyTV is clicked, SKYTV movies show.

I also have a range slider, which represents the maximum run time.

I have 2 pages (see below) - submit.php and ajax.html

Problem:

On the HTML page, there is a slider. When the user moves the slider, I'd like this to represent a change in the maximum amount of runtime allowed on the return. Runtime is stored within both tables.

The slider within the ajax.html is:

<script>
var slider = document.getElementById("runtime");
var output = document.getElementById("runtime_");
output.innerHTML = slider.value;

slider.oninput = function() {
output.innerHTML = this.value;
}
</script>

The slider is as:

<div class="slidecontainer">
<input type="range" min="1" max="360" value="120" class="slider" id="runtime">
<p>Runtime: <span id="runtime_"></span></p>

This is the script within the ajax.html that creates the table and returns the HTML values to send to submit.php. Please excuse that the functions say "employees", I was following a tutorial.

<script>
  function makeTable(data){
    var tbl_body = "";
    for (var i = 0; i < data.length; i++)  {
      var tbl_row = "";
      var t = i;
      for (var j=0; j<2; j++) {
      //tbl_row +=("<td>" + data[i].tmdbid + "</td>");
        tbl_row +=("<td><IMG SRC='my link goes here"+ data[i].poster +"'></td>");
        i++;
      }
      tbl_body += "<tr>"+tbl_row+"</tr>" 
    }
    return tbl_body;
  }
  function getEmployeeFilterOptions(){
    var opts = [];
    $checkboxes.each(function(){
      if(this.checked){
        opts.push(this.name);
      }
    });
    var slider = document.getElementById("runtime");
    opts.push(slider.value);
    return opts;
  }
  function updateEmployees(opts){
    $.ajax({
      type: "POST",
      url: "submit.php",
      dataType : 'json',
      cache: false,
      data: {
        filterOpts: opts
      },
      success: function(records){
        $('#employees tbody').html(makeTable(records));
      }
    });
  }
  var $checkboxes = $("input:checkbox");
  $checkboxes.on("change", function(){
    var opts = getEmployeeFilterOptions();
    updateEmployees(opts);
  });
  updateEmployees();
</script>

My submit page then creates SQL based on the return, for the slider I currently have:

if (in_array("runtime", $opts)) {
  $where .= ' AND runtime < VALUE OF THE SLIDER?????';
}

The expected result is that the movement of the slider will change the SQL return for WHERE runtime < value of the slider.

  • 写回答

1条回答 默认 最新

  • dongzhao3040 2019-05-01 10:53
    关注

    In your code, the range input's value is sent in one array with the checkbox names, so with more numeric data it would be difficult to tell which value is the one we need for the SQL query. The data from the inputs can be sent in a more associative way:

    function getEmployeeFilterOptions(){
    // here we're creating an object instead of an array
      var opts = {
        checkboxes: [],
        sliderValue: null
      };
      $checkboxes.each(function(){
        if(this.checked){
          opts.checkboxes.push(this.name);
        }
      });
      var slider = document.getElementById("runtime");
      opts.sliderValue = slider.value;
      return opts;
    }
    function updateEmployees(opts){
      $.ajax({
        type: "POST",
        url: "submit.php",
        dataType : 'json',
        cache: false,
        data: opts,
        success: function(records){
          console.log(records);
          $('#employees tbody').html(makeTable(records));
        }
      });
    }
    // we'll sent an event listener for all inputs so the updateEmployees function will be invoked also after the slider is moved
    var $checkboxes = $("input");
    $checkboxes.on("change", function(){
      var opts = getEmployeeFilterOptions();
      updateEmployees(opts);
    });
    

    This way the data passed in the AJAX request will look like that:

    {checkboxes: Array(1), sliderValue: "120"}
    

    Then, on the back-end you will be able to modify your SQL query like so:

    $checkboxes = $_POST["checkboxes"];
    $slider_value = $_POST["sliderValue"];
    if (in_array("runtime", $checkboxes)) {
        $where .= ' AND runtime < '  . $slider_value;
    }
    

    With the range input value being set to, for example 273, the $where variable will hold:

    AND runtime < 273
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取