简体   繁体   English

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

[英]How to add slider in the JSON AJAX request from HTML

I've got 2 tables that are storing movie ID, posters and run-times from SKYTV and NOWTV. 我有2张桌子可以存储来自SKYTV和NOWTV的电影ID,海报和运行时间。 They hold and ID number and a poster path. 他们持有身份证号码和海报路径。 When the NOWTV checkbox is clicked, NOWTV movies show. 点击NOWTV复选框后,NOWTV电影会显示。 When SkyTV is clicked, SKYTV movies show. 点击SkyTV时,SKYTV电影会显示。

I also have a range slider, which represents the maximum run time. 我还有一个范围滑块,代表最大运行时间。

I have 2 pages (see below) - submit.php and ajax.html 我有2页(见下文) - submit.php和ajax.html

Problem: 问题:

On the HTML page, there is a slider. 在HTML页面上,有一个滑块。 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: ajax.html中的滑块是:

<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. 这是ajax.html中的脚本,它创建表并返回要发送到submit.php的HTML值。 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: 我的提交页面然后根据返回创建SQL,对于我目前拥有的滑块:

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. 预期的结果是滑块的移动将改变WHERE runtime <滑块值的SQL返回值。

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. 在您的代码中,范围输入的值在一个带有复选框名称的数组中发送,因此使用更多数值数据时,很难确定哪个值是SQL查询所需的值。 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: 这样,在AJAX请求中传递的数据将如下所示:

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

Then, on the back-end you will be able to modify your SQL query like so: 然后,在后端,您将能够像这样修改您的SQL查询:

$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: 将范围输入值设置为,例如273$where变量将保持:

AND runtime < 273

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM