如何将 ajax 搜索与另一个 ajax 搜索结合起来
我有一个 ajax 中的日期时间选择器,可以按时间间隔过滤表中的数据。 我想添加一个文本过滤器,因此计划是同时按日期和文本进行过滤。
当用户写入他想查看的记录名称时,是否有保留日期过滤器的方法?
这基本上是一个双重过滤器,但我不知道如何让它们同时工作
<script>
$(document).ready(function(){
$.datepicker.setDefaults({
dateFormat: 'yy-mm-dd'
});
$(function(){
$("#from_date").datepicker();
$("#to_date").datepicker();
});
$('#filter').click(function(){
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();
if(from_date != '')
{
$.ajax({
url:"filter.php",
method:"POST",
data:{from_date:from_date, to_date:to_date},
success:function(data)
{
$('#order_table').html(data);
}
});
}
else
{
alert("Selecione uma data");
}
});
});
filter.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$data_i = filter_input(INPUT_POST, 'from_date');
$data_f = filter_input(INPUT_POST, 'to_date');
} else {
header("location:erro.php");
exit();
}
if (empty($data_f)) {
$data_f = date('y-m-d');
}
$connect = mysqli_connect("localhost", "root", "", "layouts");
$output = '';
$query = "SELECT m.nome AS Máquina, r.cod_máquina AS ID, r.cod_posto AS Posto, r.data AS Data, r.tipo AS Tipo, r.desc_alt AS Descr, r.responsável AS Responsável FROM registos_alteração r, máquinas m WHERE r.cod_máquina = m.cod_máquina AND r.data BETWEEN '".$_POST["from_date"]."' AND '$data_f'";
$result = mysqli_query($connect, $query);
$output .= '
<table align="center">
<thead>
<tr>
<font color="black">
<th>Máquina</th>
<th>Nº SAP</th>
<th>Posto</th>
<th>Data</th>
<th>Tipo</th>
<th>Descrição Alteração</th>
<th>Responsável</th>
</tr>
</thead>
<tbody>
';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$data = $row['Data'];
$DateTime = DateTime::createFromFormat('Y-m-d', $data);
$data_formatada = $DateTime->format('d-m-Y');
$output .= '
<tr>
<td>'. $row["Máquina"] .'</td>
<td>'. $row["ID"] .'</td>
<td>'. $row["Posto"] .'</td>
<td>'. $data_formatada .'</td>
<td>'. $row["Tipo"] .'</td>
<td>'. $row["Descr"] .'</td>
<td>'. $row["Responsável"] .'</td>
</tr>
';
}
}
else
{
echo '<script language="javascript">';
echo 'alert("Não existem resultados na data especificada!");';
echo 'window.location.reload();';
echo '</script>';
}
$output .= '</table>';
echo $output;
?>
I have a datetimepicker in ajax that filters my data in the table by an interval of time.
I'd like to add a text filter to, so the plan is to filter by date and text at same time.
Is a way of keep the date filter when user writes the name of the record that he wants to see?
That's basically a double filter but I don't know how to make them work at same time
<script>
$(document).ready(function(){
$.datepicker.setDefaults({
dateFormat: 'yy-mm-dd'
});
$(function(){
$("#from_date").datepicker();
$("#to_date").datepicker();
});
$('#filter').click(function(){
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();
if(from_date != '')
{
$.ajax({
url:"filter.php",
method:"POST",
data:{from_date:from_date, to_date:to_date},
success:function(data)
{
$('#order_table').html(data);
}
});
}
else
{
alert("Selecione uma data");
}
});
});
filter.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$data_i = filter_input(INPUT_POST, 'from_date');
$data_f = filter_input(INPUT_POST, 'to_date');
} else {
header("location:erro.php");
exit();
}
if (empty($data_f)) {
$data_f = date('y-m-d');
}
$connect = mysqli_connect("localhost", "root", "", "layouts");
$output = '';
$query = "SELECT m.nome AS Máquina, r.cod_máquina AS ID, r.cod_posto AS Posto, r.data AS Data, r.tipo AS Tipo, r.desc_alt AS Descr, r.responsável AS Responsável FROM registos_alteração r, máquinas m WHERE r.cod_máquina = m.cod_máquina AND r.data BETWEEN '".$_POST["from_date"]."' AND '$data_f'";
$result = mysqli_query($connect, $query);
$output .= '
<table align="center">
<thead>
<tr>
<font color="black">
<th>Máquina</th>
<th>Nº SAP</th>
<th>Posto</th>
<th>Data</th>
<th>Tipo</th>
<th>Descrição Alteração</th>
<th>Responsável</th>
</tr>
</thead>
<tbody>
';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$data = $row['Data'];
$DateTime = DateTime::createFromFormat('Y-m-d', $data);
$data_formatada = $DateTime->format('d-m-Y');
$output .= '
<tr>
<td>'. $row["Máquina"] .'</td>
<td>'. $row["ID"] .'</td>
<td>'. $row["Posto"] .'</td>
<td>'. $data_formatada .'</td>
<td>'. $row["Tipo"] .'</td>
<td>'. $row["Descr"] .'</td>
<td>'. $row["Responsável"] .'</td>
</tr>
';
}
}
else
{
echo '<script language="javascript">';
echo 'alert("Não existem resultados na data especificada!");';
echo 'window.location.reload();';
echo '</script>';
}
$output .= '</table>';
echo $output;
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用要发送到 ajax 的返回数据创建一个函数(如 getFilteredData)。这样函数就可以在请求之前过滤ajax数据。
您可以像这样过滤数据库查询:
You can create a function (like getFilteredData) with the return data you want to send to ajax. This way the function filters the ajax data before the request.
After you can filter the DB Query like this: