mardi 21 juillet 2020

Multiple Filters Selected Remove Duplicate Results From Ajax Call

I am getting duplicate results in my table because of AJAX calls not SQL queries. I need to return results from my database based on checkbox filters I have on my HTML page. I have three filter categories: skills, certs, and education each residing in different tables. The problem is every time a new checkbox is selected, I get a new result set.

For instance if I check skill1 I get a result with skill1 but if I check skill1 and skill2. I get the result set from skill1 and then for skill1 and 2 which is 2 results.

The query itself is giving me what I want, but the query is called multiple times because of the ajax call I when a checkbox is selected. That checkbox value is sent as value I later use in my query. The problem is I only want the result of the most checkboxes checked. Going back to my precious example if skill 1 and skill 2 are checked, I only want the result set for skill1 and skill2, not skill1 and then skill1 and skill2 depending on the order in which I check the checkboxes. My relevant code is below:

                  function get_filter(class_name){
                            var filter = [];
                            $('.'+class_name+':checked').each(function(){
                            filter.push($(this).val());
                            }); 
                            return filter;
                  }
                  function filter_data(){
                            var action = 'fetch_data';
                            var skills = get_filter('skills');                           
                            var certificate= get_filter('certificates');                           
                            var degree = get_filter('degree');
                            return new Promise(function(resolve, reject){
                                $.ajax({
                                    url:"fetch_data1.php",
                                    method:"POST",
                                    data:{action:action, skills:skills, certificate:certificate, degree:degree},
                                    success:function(data){
                                        $('.filter_data').html(data);
                                        //console.log($('.filter_data'));
                                        
                                    }
                                });
                 }

And here is some relevant code from fetch_data1.php:

 foreach ($_POST["skills"] as &$value){
    $skills[] = $value;
 }

$ids = join("','",$skills);  
$joinedSkills = getJoinedSkills($ids);

    while($row=$joinedSkills->fetch_assoc()){
        
            $emp_id = $row["emp_id"];
            if ($emp_id != $_POST["empId"]){
            echo "<tr class='filterRows'><td>{$row["emp_first_name"]}</td><td>{$row["emp_last_name"]}</td>
                  <td>{$row["emp_degree"]}</td><td>{$row["emp_exp_yrs"]}</td>
                <td><a href='hrform2.php?emp_id=".$row["emp_id"]."'><button class='btn btn-warning btn-sm'  method='get' id='$row[emp_id]' onclick='myFunction()'>Edit</button></a></td>
                <td><a href='view.php?emp_id=".$row["emp_id"]."'><button class='btn btn-warning btn-sm' method='get' id='$row[emp_id]' onclick='myFunction()' >View</button></a></td>
                <td hidden><input class='empInput' name='empId' value={$row["emp_id"]}></td></tr>";
            }
            //$employeeCount = 0;
    }



Aucun commentaire:

Enregistrer un commentaire