PHP - 选中复选框后,发送请求以在数据库上运行查询

时间:2017-04-04 14:40:23

标签: javascript php ajax

所以我一直在研究这段代码已经有一段时间了,我已经做了很多调试但是无法解决这个问题。我想要做的是:如果选中了复选框,则发送请求以在mySQL数据库上运行查询FROM项目WHERE .class(复选框)'<' this.value(再次显示复选框)然后获取过滤后的结果,然后使用我的javascript格式化它:

的index.php:

<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>

这是我正在调用的PHP文件showItems.php(相关部分):

    //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>

因此,我希望每次单击Index.php文件中的复选框时都会运行此代码,以便我可以获取更新的过滤项$ itemList,但我无法弄清楚如何执行此操作。我为测试这个做的事情是将我的php值存储为javascript变量,包括showItems.php然后在Index.log中将来自ShowItems.php的变量控制在console.log中,并且在点击时查询没有更新有意义我猜测。在AJAX成功函数中,'data'包含带有更新查询的整个HTML源代码,但我无法弄清楚如何仅使用成功函数中需要的特定代码。任何想法都会有所帮助。

1 个答案:

答案 0 :(得分:0)

尝试这样做:

  • $Cred = Get-Credential $computers = Get-Content c:\test\file.txt Invoke-Command -ComputerName $computers -Credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c 'ipconfig'"} | Out-File c:\test\output.txt -Append 转到on("change",...)
  • 同时尝试使用而不是on("click",...)this.checked,这将返回true或false,具体取决于是否选中复选框。
  • 您可能想要更改选择器或复选框类,因为它们都是相同的,并且可以为您提供不需要的功能,以便在您单击复选框时获取值,因为选择器会同时找到两个复选框。

希望这些想法可以让你更接近你想要的地方。 干杯