I wanted to create a form where a user can search the records of a certain person with a specific month. Here's my form:
<div class="form-row">
<div class="col-sm-4">
<select class="form-control" name="name" id="name">
<option selected="selected" style="display:none" value="">Select Employee</option>
<?php echo fill_employees($connect); ?> <!---option list--->
</select>
</div>
<div class="col-sm-2">
<select class="form-control" name="month" id="month">
<option selected="selected" style="display:none" value="0">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
</div>
<div class="col-sm-2">
<select class="form-control" name="year" id="year">
<option selected="selected" style="display:none" value="">Year</option>
<?php echo fill_year($connect); ?> <!---option list--->
</select>
</div>
</div>
<div class="row" id="show_data">Search Results</div>
Below is my script:
<script>
$(document).ready(function(){
$('#name'),$('#month'),$('#year').change(function(){
var name = $(this).val();
var month = $(this).val() ;
var year = $(this).val();
$.ajax({
url:"search.php",
method:"POST",
data:{name:name,month:month,year:year},
success:function(data){
$('#show_data').html(data);
}
});
});
});
</script>
and here is my search.php file
<?php
$connect = mysqli_connect("localhost", "root", "", "test");
$output = '';
echo '<div class="fixed-header col-sm-12">';
echo '<table class="table table-hover table-sm">';
echo '<thead class="thead-dark">';
echo '<th style="width:15%; text-align:center;">Day</th>';
echo '<th style="width:25%; text-align:center;">Date</th>';
echo '<th style="width:20%; text-align:center;">Time In</th>';
echo '<th style="width:20%; text-align:center;">Time Out</th>';
echo '<th style="width:20%; text-align:center;">Total Hours</th>';
echo '</thead>';
$qname = "SELECT * FROM employees";
$valid_nm = array($qname);
$valid_mo = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12');
$qyear = "SELECT year(timeIn) FROM attendance GROUP BY year";
$valid_yr = array($qyear);
$q = "SELECT name, timeIn, timeOut,
date(timeIn) as date,
month(timeIn) as month,
year(timeIn) as year,
TIMESTAMPDIFF(MINUTE, timeIn, timeOut)/60 AS total_hrs
FROM attendance";
// initialize array for WHERE clause conditions
$where = array('TRUE');
if (in_array($_POST['name'], $valid_nm))
{
$where[] = 'name = "' . $_POST['name'] . '"';
}
if (in_array($_POST['month'], $valid_mo))
{
$where[] = 'month(timeIn) = "' . $_POST['month'] . '"';
}
if (in_array($_POST['year'], $valid_yr))
{
$where[] = 'year(timeIn) = "' . $_POST['year'] . '"';
}
$output = '';
$sql = 'SELECT name, timeIn, timeOut,
date(timeIn) as date,
month(timeIn) as month,
year(timeIn) as year,
TIMESTAMPDIFF(MINUTE, timeIn, timeOut)/60 AS total_hrs
FROM attendance
WHERE ' . implode(' AND ', $where);
$result = mysqli_query($connect, $sql);
while ($row = mysqli_fetch_array($result))
{
$output .= '<tr>';
$output .= '<td style="width:15%; text-align:center;">'. date('l', strtotime($row["timeIn"])) .'</td>';
$output .= '<td style="width:25%; text-align:center;">'. date('d-M-Y', strtotime($row["timeIn"])) .'</td>';
$output .= '<td style="width:20%; text-align:center;">'. date('h:i A', strtotime($row["timeIn"])) .'</td>';
$int = strtotime($row["timeOut"]);
if ($int < 0)
{
$output .= '<td style="width:20%; text-align:center">NA</td>';
$output .= '<td style="width:20%; text-align:center; color:red">NA</td>';
} else {
$output .= '<td style="width:20%; text-align:center;">'. date('h:i A', strtotime($row["timeOut"])) .'</td>';
$output .= '<td style="width:20%; text-align:center;">'. number_format($row['total_hrs'],2) .'</td>';
};
$output .= '</tr>';
}
echo $output;
echo '</table>';
echo '</div>';
?>
It supposedly needs to show the results of a certain person and selected month and year but instead, it is showing ALL the data on my table. I think the mistake is on my criteria but I don't know where exactly. This is my first attempt at AJAX.