您的代码没有form 标签,也没有submit 按钮。您可以使用@Mark M 参考的问题中的链接。
现在,让我们来看看如何使用组合框来做到这一点。
您的 HTML 表单如下所示:
<form method="GET">
<select name="sort" id="sort">
<option value="1" <?php if($selection=="ORDER BY id DESC"){echo "selected";} // if orderd by newest, selecte this ?> >Newest To Oldest</option>
<option value="2" <?php if($selection=="ORDER BY id ASC"){echo "selected";} // if orderd by newest, selecte this ?> >Oldest To Newest</option>
</select>
<input type="submit" name="submit"/>
</form>
那么您的 PHP 脚本将如下所示:
<?php
$selection=isset($_GET['sort']) ? $_GET['sort'] : "";
if($selection == "1"){$selection="ORDER BY id DESC";} // if selection equals to 1 then sort by newest
else if($selection == "2"){$selection="ORDER BY id ASC";} // else if selection equals to 2 then sort by oldest
else{$selection="ORDER BY id DESC";} // else just sort by newest
$sql="SELECT id, threadName, message FROM threads "; // don't forget space the end of sql
$sql.=$selection;
//echo $sql; //$sql will look like "SELECT id, threadName, message FROM threads ORDER BY id ASC "
//mysqli_query then fetch_assoc then do your loop
?>
如果您打算查询combo-box 的更改,您可以借助Javascript(jQuery) 和Ajax 来完成。看看这个和平的代码:
<script>
$(document).on('change', '#sort', function(){ // if combo-box changes do a Ajax call
$.ajax({
url:"url/to/script.php", // url to your php script
type:"GET", // method to use
data:{sort: $("#sort").val()}, // parameters to send
success: function(data){ // if this ajax call(not script) is successful then
$('#selector').html(data); // add the returned data to the DOM
}
});
});
</script>