>我有一个基本的SQL查询,它根据输入到搜索字段中的内容获取一些数据 - 我放了一个从数据库中的列填充的选择框 - 我想做的是,当它被更改/发布时,它会更新已经找到的结果,即通过游戏的"类型"进一步钻取它们。
这是我已有的代码
echo "<select id='dropdown' name='dropdown' class='dropdown'>";//creates select HTML element
$stmt = $dbh->prepare('SELECT genre_name FROM genre'); //prepares sql query
$stmt->execute(); //executes query
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { //fetches data in associative array
echo "<option>{$row['genre_name']}</option>"; //inputs data found into select as an option
}
echo "</select>";
echo '<input type="submit" value="Filter" id="filter" name="filter" class="filter">';
if(!isset($_POST['filter']))//check if filter genre has been pressed
{
echo ("<h4>Please select a genre</h4>"); //if not show this
}
else { //otherwise if it has...do this
echo ("<h4>Your results have been filtered</h4>");
$dropvalue = $_POST['dropdown']; //sets variable of value of dropdown box
$dbh = config();
//sorting function, want to further drill search results by genre
$stmt = $dbh->prepare("SELECT * FROM consoles, games, genre WHERE genre.genre_name = :dropvalue");
$stmt->bindValue(':dropvalue','%'.$dropvalue.'%');
$stmt->execute();
while ($row = $stmt->fetch())
{
echo "<ul>";
echo "<a href='details.php?game_id=".$row['game_id']."'>".$row['game_name']."</a>";
echo "</ul>";
}
}
$dbh = NULL; //terminates connection to database
?>
你可以使用 Javascript 来做到这一点。只需做 :
1) 在标签前添加<form action="" method="post" id="drop_submit">
<select>
2)编写<select id="select_tag" name="YOUR_SELECT_NAME">
"PHP DB代码选项"
</select>
3) 添加以下 javascript 以在 HTML 页面中的任何位置提交表单。
<script type="text/javascript">
$('#status').change(function ()
{
$('#drop_submit').submit();
});
</script>
是的,别忘了补充,
if(isset($_POST['dropdown']))
{
$dropdown = $_POST['dropdown'];
}
else
{
$dropdown = "YOUR_DEFAULT / 1ST VALUE";
}
$stmt = $dbh->prepare("SELECT game_name FROM games WHERE games.game_id=('$dropdown')");
//prepares sql statement, this prevents SQL Injection, as the query is sent seperate to the string entered.
$stmt->bindValue(':dropdown','%'.$dropdown.'%');
$stmt->execute();
当您更新问题时,这是解决方案
echo '<form action="" method="post">';
echo "<select id='dropdown' name='dropdown' class='dropdown'>";//creates select HTML element
$stmt = $dbh->prepare('SELECT genre_name FROM genre'); //prepares sql query
$stmt->execute(); //executes query
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { //fetches data in associative array
echo "<option>{$row['genre_name']}</option>"; //inputs data found into select as an option
}
echo "</select>";
echo '<input type="submit" value="Filter" id="filter" name="filter" class="filter">';
echo '</form>';
if(!isset($_POST['filter']))//check if filter genre has been pressed
{
echo ("<h4>Please select a genre</h4>"); //if not show this
}
else { //otherwise if it has...do this
echo ("<h4>Your results have been filtered</h4>");
$dropvalue = $_POST['dropdown']; //sets variable of value of dropdown box
$dbh = config();
//sorting function, want to further drill search results by genre
$stmt = $dbh->prepare("SELECT * FROM consoles, games, genre WHERE genre.genre_name = :dropvalue");
$stmt->bindValue(':dropvalue','%'.$dropvalue.'%');
$stmt->execute();
while ($row = $stmt->fetch())
{
echo "<ul>";
echo "<a href='details.php?game_id=".$row['game_id']."'>".$row['game_name']."</a>";
echo "</ul>";
}
}
$dbh = NULL; //terminates connection to database
?>