I am trying to create a form that utilizes PHP and Jquery AJAX form submission mechanism which 'filters' data from a mySQL database.
There are three dropdown bars in the form with a 'submit' button, which will 'filter' the data from the database. However, it is not a requirement of the user to want to filter from all the choices in the dropdowns - 'cuisine', 'pricing', 'location'. I need to create some sort of a query where if a dropdown is not being used, it is not used in the filter.
Below is what I've started on. I've only made the 'cuisine' filter functional at the moment.
if (isset($_GET['cuisine']) && isset($_GET['pricing'])) {
$cuisine = $_GET['cuisine'];
$pricing = $_GET['pricing'];
$location = $_GET['location'];
if ($cuisine != 'Cuisine') {
$cuisineUse = true;
} else { $cusineUse = false; }
if ($pricing != 'Price Range') {
$pricingUse = true;
} else { $pricingUse = false; }
if ($location != 'Location') {
$locationUse = true;
} else { $locationUse = false; }
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM restaurants
WHERE Cuisine='$cuisine'
ORDER BY restaurantID
")
or die(mysql_error());
And the Jquery:
<script>
/* attach a submit handler to the form */
$("#searchForm").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
term = $form.find( 'input[name="cuisine"]' ).val(),
url = $form.attr( 'action' );
/* Send the data using post and put the results in a div */
$.post( url, { s: term },
function( data ) {
var content = $( data ).find( '#content' );
$( "#jsDiv" ).empty().append( content );
}
);
});
</script>
P.S The user could also use two or more of the 'filters' at once.