1

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hmm.. How would this work if multiple of them were in play. For example, if the user wanted to filter them by cuisine and price? or location and price? etc...
1

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";

1 Comment

I don't quite understand what's going on with this one. Can you explain it a little more please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.