Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Auflisten von Datenbankwerten gemäß dem ausgewählten Filter in der Dropdown-Liste

html:

<select name="filter" onchange="filter(this.value)">
  <option>FILTER:</option>
  <option value="alphabetical">ASC</option> 
  <option value="date">Date</option> 
</select>
<div id="results"></div>// store the results here

Jquery:

function filter(item){
$.ajax({
type: "POST",
url: "filter.php",
data: { value: item},
success:function(data){
  $("#results").html(data);
}
});
}

filter.php:

include "connection.php";  //database connection
$fieldname = $_POST['value'];
 if($fieldname=="alphabetical"){
  // if you choose first option
  $query1 = mysqli_query("SELECT * FROM table ORDER BY name ASC"); 
  // echo the results
  }else{
  // if you choose second option
  $query1 = mysqli_query("SELECT * FROM table ORDER BY date ASC");
  // echo the results
}

Hinweis: Vergessen Sie nicht, die jquery-Bibliothek einzubinden.