title: Ajax Search with select dropdown filters CakePhp
body: <p>I want to build an ajax searc filters with dropdown select boxes. 
  I will try to be as much clear as i can. </p>
  
  <p>I have a Typology Model and controller. A typology has</p>
  
  <p><strong>id | title | description | item_id | condition_id | category_id | condition_id | price</strong></p>
  
  <p>where <strong>id</strong> is primary key and <strong>item_id, condition_id, category_id, condition_id</strong> are foregin keys. </p>
  
  <p>So when typology index page is loaded it shows a list of all typologies. </p>
  
  <p>Search Form is as follow:</p>
  
  <pre><code>&lt;form name="search_form" id="search_form"&gt;  
                  &lt;li&gt;&lt;p&gt;tipologia&lt;/p&gt;                     
                  &lt;?php    
                  echo "&lt;select name=\"typology_categories\" class=\"select_filter\" &gt;";
                  echo "&lt;option value=''&gt;All&lt;/option&gt;";
                      foreach ($typologyCategories as $typologyCategory): 
                          $category_id = $typologyCategory['TypologyCategory']['id'];
                          $category_name = $typologyCategory['TypologyCategory']['name'];
                          echo "&lt;option value='{$category_id}'&gt;{$category_name}&lt;/option&gt;";
                      endforeach;
                  echo "&lt;/select&gt;";                                   
                  ?&gt;
                  &lt;/li&gt;
  
                  &lt;li&gt;&lt;p&gt;localita&lt;/p&gt;
                  &lt;?php 
                  echo "&lt;select name=\"item_locations\" class=\"select_filter\"&gt;";
                  echo "&lt;option value=''&gt;All&lt;/option&gt;";
                      foreach ($itemLocations as $itemLocation): 
                          $item_id = $itemLocation['ItemLocation']['id'];
                          $item_name = $itemLocation['ItemLocation']['name'];
                          echo "&lt;option value='{$item_id}'&gt;{$item_name}&lt;/option&gt;";
                      endforeach;
                  echo "&lt;/select&gt;";
                  ?&gt;
                  &lt;/li&gt;
                  &lt;li&gt;&lt;p&gt;prezzo&lt;/p&gt;
                      &lt;select name="typology_price" class="select_filter"&gt;
                          &lt;option value=""&gt;All&lt;/option&gt;
                          &lt;option value="1"&gt;0 - 1,000&lt;/option&gt;
                          &lt;option value="2"&gt;1,000 - 5,000&lt;/option&gt;
                          &lt;option value="3"&gt;5,000 - 50,000&lt;/option&gt;
                          &lt;option value="4"&gt;50,000 - 100,000&lt;/option&gt;
                          &lt;option value="5"&gt;100,000 - 500,000&lt;/option&gt;
                          &lt;option value="6"&gt;500,000 - 1,000,000&lt;/option&gt;
                          &lt;option value="7"&gt;more then 1,000,000&lt;/option&gt;
                      &lt;/select&gt;
                  &lt;/li&gt;
                  &lt;li&gt;&lt;p&gt;stato&lt;/p&gt;
                  &lt;?php 
                  echo "&lt;select name=\"typology_conditions\" class=\"select_filter\"&gt;";
                  echo "&lt;option value=''&gt;All&lt;/option&gt;";
                      foreach($typologyConditions as $typologyCondition):
                          $condition_id = $typologyCondition['TypologyCondition']['id'];
                          $condition_name = $typologyCondition['TypologyCondition']['name'];
                          echo "&lt;option value='{$condition_id}'&gt;{$condition_name}&lt;/option&gt;";
                      endforeach;
                  echo "&lt;/select&gt;";
                  ?&gt;
                  &lt;/li&gt;
                  &lt;/form&gt;
  </code></pre>
  
  <p>The ajax call is:</p>
  
  <pre><code>    &lt;!-- HERE IS THE SEARCH FILTER --&gt;
      &lt;script type="text/javascript" &gt;
          $(document).ready(function () {
              $('.select_filter').bind("change keyup input",function() {
                $.ajax({
                     type: "POST",
                     url: "search.php", // This one should sent data to index action of the typology controller for processing
                     data: $("#search_form").serialize(), 
  // You will get all the select data..
                      success:function(data){
                          $("#projects").html(data);
                      }
                  });
            });
          });
      &lt;/script&gt;
  </code></pre>
  
  <p>Typology Controller is this:
  index action:</p>
  
  <pre><code>/**
   * index method
   *
   * @return void
   */
      public function index() {
          $this-&gt;set('title_for_layout', 'CESI');
          $this-&gt;layout='Homepage';
  
               // Typology Query
          $typologies = $this-&gt;Typology-&gt;find('all', array('recursive'=&gt;-1, 'order' =&gt; array('Typology.' . $this-&gt;Typology-&gt;primaryKey . ' DESC')));
          $this-&gt;set('typologies', $typologies);
  
               // Typology Category Query
          $typologyCategories = $this-&gt;TypologyCategory-&gt;find('all', array('recursive'=&gt;-1));
          $this-&gt;set('typologyCategories', $typologyCategories);
               // Typology Condition Query
  
          $typologyConditions = $this-&gt;TypologyCondition-&gt;find('all', array('recursive'=&gt;-1));
          $this-&gt;set('typologyConditions', $typologyConditions);
  
              // Item Query       
          $items = $this-&gt;Item-&gt;find('all',array('recursive'=&gt;-1));
          $this-&gt;set('items', $items);
  
              // Item Location Query
          $itemLocations = $this-&gt;ItemLocation-&gt;find('all', array('recursive'=&gt;-1));
          $this-&gt;set('itemLocations', $itemLocations);
  
              // Slider Query
          $sliders = $this-&gt;Slider-&gt;find('all', array('recursive'=&gt;-1));
          $this-&gt;set('sliders', $sliders);
      } 
  </code></pre>
  
  <p>So the Query that i wanna use for the search that i build is this:</p>
  
  <pre><code>&lt;?php 
    /* Query for the Typology*/
            $queryTypologies = "SELECT * ";
            $queryTypologies .= "FROM typologies";
            $queryTypologies .= " WHERE  1=1 ";
  
          if (isset($_POST['typology_categories']) &amp;&amp; !empty($_POST['typology_categories']) &amp;&amp; $_POST['typology_categories']!='') {
             $queryTypologies .= " AND typology_category_id = (SELECT id FROM typology_categories WHERE id ='".trim(mysql_prep($_POST['typology_categories']))."' LIMIT 1)";
          }
  
          if (trim($_POST["item_locations"])!='' &amp;&amp; !empty($_POST['item_locations'])) {
              $queryTypologies .= " AND item_id = (SELECT id FROM items WHERE item_location_id ='".trim(mysql_prep($_POST['item_locations']))."' LIMIT 1)";   //'".trim(mysql_prep($_POST['item_locations']))."'
          }
                              // value="1" 0         - 1,000
                              // value="2" 1,000     - 5,000
                              // value="3" 5,000     - 50,000
                              // value="4" 50,000    - 100,000
                              // value="5" 100,000   - 500,000
                              // value="6" 500,000   - 1,000,000
                              // value="7" more then - 1,000,000
  
          if (trim($_POST["typology_price"])!='' &amp;&amp; !empty($_POST['typology_price']) &amp;&amp; $_POST['typology_price']!='') {
  
              if (trim($_POST["typology_price"]) == 1) {  
                  $queryTypologies .= " AND price &lt;= 1000";                           
              }
              if (trim($_POST["typology_price"]) == 2) {  
                  $queryTypologies .= " AND price BETWEEN 1001 AND 5000";                         
              }
              if (trim($_POST["typology_price"]) == 3) {  
                  $queryTypologies .= " AND price BETWEEN 5001 AND 50000";                            
              }
              if (trim($_POST["typology_price"]) == 4) {  
                  $queryTypologies .= " AND price BETWEEN 50001 AND 100000";                          
              }
              if (trim($_POST["typology_price"]) == 5) {  
                  $queryTypologies .= " AND price BETWEEN 100001 AND 500000";                         
              }
              if (trim($_POST["typology_price"]) == 6) {  
                  $queryTypologies .= " AND price BETWEEN 500001 AND 1000000";                            
              }
              if (trim($_POST["typology_price"]) == 7) {  
                  $queryTypologies .= " AND price &gt; 1000000";                         
              }
          }
  
  
          if (trim($_POST["typology_conditions"])!='' &amp;&amp; !empty($_POST['typology_conditions']) &amp;&amp; $_POST['typology_conditions']!='') {
              $queryTypologies .= " AND typology_condition_id = '".trim(mysql_prep($_POST['typology_conditions']))."' ";  
          }
  
            $typologies = mysql_query($queryTypologies) or die (mysql_error());
  
              $numrows = mysql_num_rows($typologies);
                          if($numrows != 0){
                                  $result ="";
                                      while($rowTypologies= mysql_fetch_assoc($typologies)){
  
                                          $id                    = $rowTypologies['id'];
                                          $item_id               = $rowTypologies['item_id'];
                                          $title                 = htmlentities($rowTypologies['title'], ENT_COMPAT, "UTF-8");
                                          $description           = htmlentities($rowTypologies['description'], ENT_COMPAT, "UTF-8");
                                          $thumbnail             = htmlentities($rowTypologies['thumbnail'], ENT_COMPAT, "UTF-8");
                                          $price                 = htmlentities($rowTypologies['price'], ENT_COMPAT, "UTF-8");
                                          $typology_category_id  = htmlentities($rowTypologies['typology_category_id'], ENT_COMPAT, "UTF-8");
                                          $typology_condition_id = htmlentities($rowTypologies['typology_condition_id'], ENT_COMPAT, "UTF-8");
  
                                           if (strlen($description)&gt;303) {
                                              $short_description = substr($description, 0, 300);
                                              $description = $short_description."...";
                                           }
  
                                          $first = base64_encode($item_id);
                                          $typologyThumbnails = "admin/app/webroot/img/uploads/typology/thumbnails/" . $thumbnail;
                                          $result .= "&lt;div class=\"item_shadow\"&gt;";
                                          $result .= "&lt;div class=\"item\" style=\"background-image:url({$typologyThumbnails});\"&gt;";
                                          $result .=  "&lt;div class=\"item-content\"&gt;";
                                          $result .=      "&lt;div class=\"item-top-content\"&gt;";
                                          $result .=          "&lt;div class=\"item-top-content-inner\"&gt;";
                                          $result .=              "&lt;div class=\"item-top-title\"&gt;";
                                          $result .=                      "&lt;h4&gt;{$title}&lt;/h4&gt;";
                                          $result .=              "&lt;/div&gt;";
                                          $result .=          "&lt;/div&gt;"    ;
                                          $result .=      "&lt;/div&gt;";
                                          $result .=      "&lt;div class=\"item-add-content\"&gt;";
                                          $result .=          "&lt;div class=\"item-add-content-inner\"&gt;";
                                          $result .=              "&lt;div class=\"description-inner\"&gt;";
                                          $result .=                  "&lt;p&gt;{$description}&lt;/p&gt;";
                                          $result .=              "&lt;/div&gt;";
                                          $result .=              "&lt;div class=\"read-more-inner\"&gt;";
                                          $result .=                  "&lt;a href=\"new.php?id=$first\"&gt;maggiori informazioni &lt;img src=\"img/elenco.png\"/&gt;&lt;/a&gt;";
                                          $result .=              "&lt;/div&gt;";
                                          $result .=          "&lt;/div&gt;";
                                          $result .=      "&lt;/div&gt;";
                                          $result .=  "&lt;/div&gt;";
                                          $result .= "&lt;/div&gt;";
                                          $result .= "&lt;/div&gt;";
  
                                      }
                                  echo $result;
                              } else {
                              echo "No Result Found in Database.";
                          }    
   ?&gt;
  </code></pre>
  
  <p>I  know this works because first i build it on raw php coding as you can see by yourselfs, and it worked perfect but now i want to implement it on cakephp. And i havent found a way yet.</p>
  
  <p>Any help would be much appruciated!</p>
  
  <p>Thanx in advance</p>