Now the searchApp.php. You first have to check if all the conditions, passed over by ajax call, exist. Then you escape the values, to be safe. Next you construct the query without any conditions. Then you check if any of conditions exist and add WHERE and AND statements. In this case we have only one possible condition: the category. You can do a bit of error checking too, I have left it out for clarity. See the comments in the code.
<?php
// the database connection has to be setup here (the link has to exist)!
require_once '../connect.php';
// check if all the conditions exist
// (for now check only for search_term and category, later add other drop downs)
if(
isset($_POST['search_term']) &&
!empty($_POST['search_term']) &&
isset($_POST['category'])
) {
$search_term = mysql_real_escape_string($_POST['search_term']);
$category = mysql_real_escape_string($_POST['category']);
// construct the query for all categories first
$query = "SELECT Type FROM autosuggest WHERE Type LIKE '$search_term%'";
// then if category is other than all add the condition
if($category != 'all') {
$query .= " AND Category LIKE '$category'";
}
// die($query);
$resource = mysql_query($query);
while(($row=mysql_fetch_assoc($resource))) {
echo '<li><b>',$row['Type'],'</b></li>';
}
}
?>