Let's tidy this up and simplify it a bit...
function updateResults() {
query = {"town_id": $('#town_id').val(),
"value_type": $('#filter_type').val(),
"value_date": $('#filter_date').val()
}
$.ajax({
type: "POST",
url: "system/live_filter.php",
data: query,
cache: false,
success: function(html){
$("#results").html(html);
}
});
};
and then in the PHP:
require_once 'db.php';
// Define Output HTML Formating
$html = '';
$html .= '
$html .= '
titleString
';$html .= '
typeString
';$html .= '
dateString
';$html .= '
$town_id = $_REQUEST['town_id'];
$type = $_REQUEST['value_type'];
$date = $_REQUEST['value_date'];
// Prepare values for database results query
$town_id = $db->real_escape_string($town_id);
$type = $db->real_escape_string($type);
$date = $db->real_escape_string($date);
// Build Query
$query = "SELECT * FROM events WHERE towns_id='$town_id' AND type='$type' AND date>='$date'";
/*Should it definitely be towns_id and not town_id?*/
// Do Search
$results = $db->query($query);
while($result = $results->fetch_assoc()) {
// Insert title
$output = str_replace('titleString', $result['title'], $html);
// Insert type
$output = str_replace('typeString', $result['type'], $output);
// Insert date
$output = str_replace('dateString', $result['date'], $output);
// Output
echo($output);
}
Of course if you're willing to move the template, it gets even simpler...
require_once 'db.php';
$town_id = $db->real_escape_string($_REQUEST['town_id']);
$type = $db->real_escape_string($_REQUEST['value_type']);
$date = $db->real_escape_string($_REQUEST['value_date']);
$query = "SELECT * FROM events WHERE towns_id='$town_id' AND type='$type' AND date>='$date'";
$results = $db->query($query);
while($result = $results->fetch_assoc()) {
$html = '
$html .= '
{$result['title']}
';$html .= '
{$result['type']}
';$html .= '
{$result['date']}
';$html .= '
echo $html;
}
As to why it's erroring...
Firstly, are you sure the variables are being populated? The error you gave in comments would occur if town_id was missing. Since you're not quoting that field, it would result in broken SQL. It also makes the escaping pointless as the output expects to be in quotes.
I'd also check that the format of the date coming from your form is one that your database understands...
Try changing the PHP to be as follows:
require_once 'db.php';
$town_id = $db->real_escape_string($_REQUEST['town_id']);
$type = $db->real_escape_string($_REQUEST['value_type']);
$date = $db->real_escape_string($_REQUEST['value_date']);
$query = "SELECT * FROM events WHERE towns_id=$town_id AND type='$type' AND date>=$date";
echo $query;
Then take the SQL it gives you and copy/paste it into your database admin tool and see what happens. Once you've fixed the syntax errors there, you'll know how to fix the query in your PHP