Refining post search results (PHP -AJAX) [closed]

-2

I would like to know how to make a vertical menu with a form to update the data without refresh and without having to press the submit. Just click on the option you want to change. Home Like if I did a search and the results appeared, and next to those results have a vertical menu, with the options available. ex:
Results of icarros,
After changing the start or end year or year, located in the side menu, it changes the search data without refresh. How to make that side menu? Help!

    
asked by anonymous 27.01.2016 / 05:56

1 answer

1

Firstly, download the jQuery library from the link link and add this file to the head of your site eg:

<head>
   <script type="text/javascript" src="path/seu arquivo.js"></script>
</head>

Create an empty file with the extension .js ex: search.js to place the code that will execute AJAX.

HTML:

<select name="filter" id="filter">
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>

Create a method in a php class in case if you are using a framework, search and return the data in JSON format. If you are using a site with structured code, create a file, for example, filter.php.

In the file search.js you can use the change event that is executed after the user selects the filter ex:

$(function(){
    $('#filter').change(function(){
        var $filtro = $(this);
        $.ajax({
        url: 'filtro.php',
      type: 'get',
      data:{
        ano: $filtro.val()
      },
      success: function(response){
         alert(response); // retorno do php
      },
      error: function(xhr){
        alert(xhr.responseText);
      }
     });
   });
});

Following this guidance in the php file, you must capitalize the request to query it as follows:

$filtro = $_REQUEST['ano']; ou $_GET['ano'];
    
27.01.2016 / 12:10