我有兩個下拉菜單,第二個下拉菜單由第一個下拉條件填充。現在,我想要同時使用兩個下拉條件並將其應用於查詢,並通過HTML將結果顯示在表中。基於兩個下拉菜單運行查詢並顯示錶格PHP
這是我的代碼,
腳本代碼:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.country').on('change', function() {
// Code to add country information in url
location.href = location.href.split('?')[0]
+ ['?country', $(this).val()].join('=');
});
});
</script>
PHP代碼的第一個下拉列表:
<?php
$countries = mysqli_query($mysqli,"select source from nhws.masterkey group by source;");
echo "<select class='country' name='country' style='width:200px'>";
echo "<option size =30 ></option>";
while($row = mysqli_fetch_array($countries)){
echo "<option value='".$row['source']."'>".$row['source']."</option>";
}
echo "</select>";
if (isset($_GET['country'])) {
$country = $_GET['country'];
echo "<p></p>";
echo "$country is the selected data set";
echo "<p></p>";
}
?>
PHP代碼第二個下拉:
<?php
if (isset($_GET['country'])) {
echo "<h5>Choose variable</h5>";
$variables = mysqli_query($mysqli,"select variable from nhws.num_all_{$country} group by variable;");
echo "<select class='variable' name='variable' style=width:200px>";
echo "<option size =30 ></option>";
while($row = mysqli_fetch_array($variables)) {
echo "<option value='".$row['variable']."'>".$row['variable']."</option>";
}
echo "</select>";
}
?>
現在,最後一個PHP代碼顯示結果表:
<?php
if (isset($_GET['variable'])) {
$results = mysqli_query($mysqli,"select q1.variable, q1.numvalue, description, num_cases
from (select variable, numvalue, count(variable) as num_cases
from nhws.num_all_{$country}
where variable = '{$variable}'
group by variable, numvalue) q1
inner join (select * from nhws.luvalues where source = '{$country}' and variable = '{$variable}')
t2 on q1.numvalue=t2.numvalue;");
echo "<h5>Counts</h5>";
if ($results->num_rows > 0) {
echo "<table><tr><th>Variable</th><th>Numvalue</th><th>Description</th><th>Num Cases</th></tr>";
// output data of each row
while($row = $results->fetch_assoc()) {
echo "<tr><td>" . $row["variable"]. "</td><td>" . $row["numvalue"]. "</td><td>" . $row["description"]. "</td><td>" . $row["num_cases"]. "</td></tr>";
}
echo "</table>";
} else {echo "0 results";}
}
?>
一旦選擇了所有的下拉選項,什麼都不會發生。查詢甚至沒有運行。看起來它並沒有通過if (isset($_GET['variable']))
聲明。
如何使它工作?
謝謝!
你從來沒有設置'$ variable'或'$ country',所以它們是null,你在做'where variable =''' - 你可能正在讀一個愚蠢的/老的教程,它假設'register_globals '仍然存在......並且請注意,你幾乎肯定容易受到[sql注入攻擊](http://bobby-tables.com) –
我沒有看到第二次下拉列表中有任何onChange事件。所以你第一次提交加載第二個,但第二個從未提交加載結果。 – Phil
那麼'$ country'就會被設置,因爲第二個下拉菜單是基於第一個下拉菜單正確填充的。所以,我使用'$ variable'的相同方法。我嘗試創建第二個下拉列表的onChange事件,但我沒有成功。你將如何進行第二次onChange事件? – user3882752