tushar747
tushar747

Reputation: 683

Filtering data with a PHP, Jquery, AJAX form mechanism and data from Database

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.

Upvotes: 1

Views: 3047

Answers (2)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

Try:


//USE mysql_real_escape_string for your variables
//is it POST or GET
        $qry = "SELECT * FROM restaurants WHERE cuisine = '".$cuisine."'";
        $qry .= empty($_POST['pricing']) ? " " : " AND pricing = '".$_POST['pricing']."'";
        $qry .= empty($_POST['location']) ? " " : " AND location = '".$_POST['location']."'";
        $qry .=" ORDER BY restaurantID";

Upvotes: 1

Shawn Janas
Shawn Janas

Reputation: 2743

You are sending a POST request so you should use $_POST instead of $_GET. Also the value of the cuisine name when sent is s and not cuisine so it should be:

if($_POST['s'] != '') {
   ...
   $result = mysql_query("SELECT * FROM restaurants WHERE Cuisine='$cuisine' ORDER BY restaurantID");
} else {
   $result = mysql_query("SELECT * FROM restaurants ORDER BY restaurantID");
}

You just need to remove the where clause if cuisine is not set.

Upvotes: 3

Related Questions