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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀