dozabg1616 2019-03-12 08:39
浏览 113
已采纳

如何从下拉列表中获取所选值并在查询中使用它而不单击提交?

I'm making a chart that will show its result based from the year that was selected from the dropdown. Is it possible to get the variable from the dropdown and use it in query without clicking submit button? I tried this code but didn't work out:

<?php
                    require '../includes/dbheader.php';
                    $query = "SELECT DISTINCT DATE_FORMAT(orderdate, '%Y') AS year
                              FROM prodsoldmonthly 
                            ";
                    $result = mysqli_query($conn, $query);  
                    echo "<select id='selectyear[]' name='selectyear' class='cd-select filter-input'>";
                    echo "<option class='dropdown' value='' selected>Choose Year</option>";
                    while($row = mysqli_fetch_assoc($result)) {
                    echo "<option class='dropdown' value='{$row['year']}'>".htmlspecialchars($row["year"])."</option>";
                    }
                    echo "</select>";

              ?> 
              <script>
              $yearselected = $("#selectyear option:selected").text(); 
              </script>
                  <!-- Products Sold per Category -- YEAR -->
                <?php  
                include('../includes/dbheader.php');
                  $query = "SELECT categoryName, qty, DATE_FORMAT(orderdate, '%Y') AS year
                            FROM prodsoldmonthly WHERE year = '$yearselected'
                            ";
                  $result = mysqli_query($conn, $query);  
                ?>   

                <script type="text/javascript">  
                   google.charts.load('current', {'packages':['corechart']});  
                   google.charts.setOnLoadCallback(drawChart);  
                   function drawChart()  
                   {  
                        var data = google.visualization.arrayToDataTable([  
                            ['categoryName', 'qty'],  
                              <?php  
                                while($row = mysqli_fetch_array($result))  
                                {  
                                echo "['".$row["categoryName"]."', ".$row["qty"]."],";  
                                }  
                                ?>  
                             ]);  
                        var options = {  
                              title: 'Products Sold Per Category by Year',  
                              is3D:true,  
                              pieHole: 0.4  
                             };  
                        var chart = new google.visualization.PieChart(document.getElementById('piechartyear'));  
                        chart.draw(data, options);  
                   }  
               </script> 
  • 写回答

1条回答 默认 最新

  • dongmi1663 2019-03-12 09:41
    关注

    You can use AJAX to launch the request with the year without having to submit the whole page:

    function queryProducts(yearselected) {
    
       var xmlhttp = new XMLHttpRequest();
    
       xmlhttp.onreadystatechange = function() {
          if (this.readyState == 4 && this.status == 200) {
             // this line will be called when the query finishes successfully
             document.getElementById("products").innerHTML = this.responseText;
          }
        };
    
        // set url with parameter
        xmlhttp.open("GET", "products.php?yearselected=" + selectedyear, true);
        xmlhttp.send();
    
    }
    

    Add an 'onchange' listener to your select to call the function while passing the selected year:

    <select id='selectyear[]' name='selectyear' onchange="queryProducts(this.value)" class='cd-select filter-input'>";
    

    Lastly, add a placeholder to display the result of the query:

    <div id="products"></div>
    

    EDIT:

    For your select statement, put the code in another page like products.php for example and call that page in the AJAX function:

    <?php
    
        // products.php
    
        include('../includes/dbheader.php');
    
        // get url parameter
        $yearselected = intval($_GET['yearselected']);
    
        $query = "SELECT categoryName, qty, DATE_FORMAT(orderdate, '%Y') AS year 
                  FROM prodsoldmonthly WHERE year = '".$yearselected."'";
    
        $result = mysqli_query($conn, $query);
    
        echo "<table>
                 <tr>
                    <th>Category</th>
                    <th>Qty</th>
                    <th>Order date</th>
                 </tr>";
        while($row = mysqli_fetch_array($result)) {
            echo "<tr>";
            echo "<td>" . $row['categoryName'] . "</td>";
            echo "<td>" . $row['qty'] . "</td>";
            echo "<td>" . $row['orderdate'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    
        mysqli_close($conn);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。