I am trying to get data from mysql table using php ajax. it showing when I select the filter from select option but when I change the select another value for filter it does not reset the table but it append new result with old result. when page load it should not filter it should filter when select filter option.
My PHP to get data from data base.
<?php
if(!empty($_POST["assign_to"])){
$filter = $_POST["assign_to"];
$sql = "SELECT * FROM projects WHERE assign_to ='".$filter ."' ";
$result = mysqli_query($connect, $sql);
$output = array();
while($row = mysqli_fetch_assoc($result))
{
$output[] = $row;
}
echo json_encode($output);
}
Filter HTML
<select id="user_ids" class="form-control" name="assign_to" required="">
<option value="">Select User</option>
<option value="85">Manager</option>
<option value="86">User 1</option>
<option value="87">User 2</option>
<option value="88">User 3</option>
</select>
Script for AJAX Call
function fetch_project_data_filtered() {
$("#user_ids").change(function(){
var assign_to = $(this).val();
var dataString = "assign_to="+assign_to;
//alert(assign_to);
$.ajax({
type: "POST",
url: "x-fetch.php",
data: dataString,
dataType:"json",
success: function(data)
{
for(var count=0; count<data.length; count++)
{
var html_data = '<tr><td>'+data[count].project_id+'</td>';
html_data += '<td data-name="project_name" class="project_name" data-type="text" data-pk="'+data[count].project_id+'">'+data[count].project_name+'</td>';
html_data += '<td data-name="created_on" class="created_on" data-type="text" data-pk="'+data[count].project_id+'">'+data[count].created_on+'</td>';
html_data += '<td data-name="target_date" class="target_date" data-type="date" data-pk="'+data[count].project_id+'">'+data[count].target_date+'</td>';
html_data += '<td data-name="assign_to" class="assign_to" data-type="text" data-pk="'+data[count].project_id+'">'+data[count].assign_to+'</td>';
html_data += '<td data-name="current_status" class="current_status" data-type="textarea" data-pk="'+data[count].project_id+'">'+data[count].current_status+'</td>';
html_data += '<td data-name="previous_status" class="previous_status" data-type="textarea" data-pk="'+data[count].project_id+'">'+data[count].previous_status+'</td>';
html_data += '<td data-name="cito_comment" class="cito_comment" data-type="textarea" data-pk="'+data[count].project_id+'">'+data[count].cito_comment+'</td>';
$('#project_data').append(html_data);
}
}
});
});
}