Javascript PHP-选中复选框时,发送对数据库运行查询的请求

Javascript PHP-选中复选框时,发送对数据库运行查询的请求,javascript,php,ajax,Javascript,Php,Ajax,因此,我已经在这段代码上工作了一段时间,我做了很多调试,但无法理解这一点。我想做的是:如果选中复选框,则发送一个请求,从WHERE.class(复选框的)“fetch_assoc()”项在mySQL数据库上运行查询 { $tempItem=新项目($row['itemID'],$row['itemName'],$row['price'],$row['description'],$row['carries'],$row['protein'],$row['choles'],$row['sodi'],

因此,我已经在这段代码上工作了一段时间,我做了很多调试,但无法理解这一点。我想做的是:如果选中复选框,则发送一个请求,从WHERE.class(复选框的)“fetch_assoc()”项在mySQL数据库上运行查询 { $tempItem=新项目($row['itemID'],$row['itemName'],$row['price'],$row['description'],$row['carries'],$row['protein'],$row['choles'],$row['sodi'],$row['picLink']); $itemList[]=$tempItem; } echo json_encode($query); ?> var检验=; var filteredList=; 因此,我希望每次单击Index.php文件中的复选框时都能运行这段代码,这样我就可以获得更新的筛选项$itemList,但我不知道如何执行此操作。为了测试这一点,我做了一些事情,将我的php值存储为javascript变量,包括showItems.php,然后是console.log,将showItems.php中的变量记录在Index.php中,并且在单击时不会更新查询,我想这是有意义的。在AJAX成功函数中,“data”包含整个HTML源代码和一个更新的查询,但是我不知道如何在成功函数中只使用我需要的特定代码。任何想法都会有帮助。

试着这样做:

  • 从“更改”上的
    ,…)
    转到“单击”上的
    ,…)
  • 还可以尝试使用代替
    this.checked
    $(this.prop(“checked”)
    ,它将根据复选框是否选中而返回true或false
  • 您可能希望更改选择器或复选框类,因为它们都是相同的,并且可以在单击复选框时为您提供不需要的功能,以便获取您的值,因为选择器会找到这两个复选框
希望这些想法能让你更接近你想去的地方。
干杯

除非我弄错了,否则我的选择器不应该是我的checkbox类的名称吗?还是我被别的东西弄糊涂了?不,这对我没有帮助,我已经能够确认onclick/onchange函数确实有效我相信需要修复的是我的AJAX代码或PHP代码是的,但是这样jQuery将找到两个复选框,因为它们具有相同的类,并且不会给您想要的结果,请尝试在
$(this).attr上执行console.log('value')
…('class')
变量,您将了解我的意思,尝试通过在浏览器开发工具的网络选项卡中使用var\u转储$\u POST变量来调试PHP,并查看它们是否正确地传递到$\u POST中。
<form>
      <label><input type="checkbox" class="calories "name="calories" value="300">Less  than 300</label><br>
      <label><input type="checkbox" class="calories" name="calories" value="500">Less than  500</label><br>    
</form>

<script>
$("input.calories:checkbox").on("change",function(){

  if(this.checked){               

    var column = $(this).attr('class'); //The class determines which column of the table is called    
    var value = $(this).attr('value'); //Takes the numeric value from the selected box
    console.log(column); 
    //$.post('showItems.php', {type: column});
    //$.post('showItems.php', {value: value});

   //Can we call the php code above to run a query using variables column and value? 
   //make a php function above and call it

    // function below will run showItemss.php?c=column?v=value
        $.ajax({
            type: "POST",
            url: "showItems.php" ,
            data: { c: column,
                    v: value},
            error: function(){console.log("error")},
            success: function(data) { 
              console.log("success");
            console.log(test);
            console.log(filteredList);
</script>
    //This array holds items from database.
$itemList = array();

//Connect and Select    
$con = makeConnection($dbhost, $dbuser, $dbpass, $dbname);

  //Get the value and type from the javascript below
  //If the type is null display the whole table
  $c = $_POST['c'];
  //echo $c;

  //$v = mysqli_real_escape_string($con,$v);
  //$type = $_POST['value'];
  if($c==null){
    $query = "SELECT * FROM items";
  }
  else{
  $v = $_POST['v'];

  $query = "SELECT * FROM items WHERE ".$c."< ".$v."";  
  }
    $result = mysqli_query($con, $query);

//Collect data from all items
    while($row = $result->fetch_assoc())
    {
        $tempItem = new Item($row['itemID'], $row['itemName'],  $row['price'], $row['description'], $row['calories'], $row['protein'], $row['choles'], $row['sodi'], $row['picLink']);

        $itemList[] = $tempItem;
    }
  echo json_encode($query);
?>

  <script>
    var test = <?php echo json_encode($query); ?>;
    var filteredList = <?php echo json_encode($itemList); ?>;
  </script>