当前位置: 首页 > 知识库问答 >
问题:

如何使用PHP MySQL AJAX添加动态过滤器

衡子琪
2023-03-14

它显示当我从选择选项中选择过滤器,但当我改变选择另一个值的过滤器它不重置表,但它追加新的结果与旧的结果它不应该过滤它应该过滤时,选择过滤器选项。

我的PHP从数据库中获取数据。

<?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);
}

过滤超文本标记语言

<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>

AJAX调用脚本

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);
                }
               }
          });

        });
    }

共有3个答案

荣俊杰
2023-03-14
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').empty().html(html_data);
            }
           }
      });

    });
}  
江凯风
2023-03-14

您可以从AJAX页面以html表格形式返回表格。

HRML选择框。

                                        <select name="scanType" id="type" class="form-control m-bot15">
                                            <option selected disabled>-select type-</option>
                                            <option value="all">All</option>
                                            <option value="USG">USG</option>
                                            <option value="CT">CT</option>
                                            <option value="X-RAY">X-RAY</option>
                                            <option value="MR">MR</option>
                                            <option value="LAB">LAB</option>
                                        </select>

在submit()上运行的JavaScript函数。

function SearchData() {
        var fromDate = $('#fromDates').val();
        var toDate = $('#toDates').val();
        var type = $('#type').val();
        var url = 'ajax_searchData.php?from='+fromDate+'&&to='+toDate+'&&type='+type;
        $.ajax({
            type: 'GET',
            url: url,
            dataType: 'html',
            success: function (data) {
                    $('#LoadTable').html(data);
            }
        })
    }

ajax\u搜索数据。php页面

<?php
include('config.php');
$fromDate = $_GET['from'];
$toDate = $_GET['to'];
$type = $_GET['type'];
$x = 1;
$total = 0;
$grandTotal = '';
$byData = mysqli_query($con,"CALL reportByDate('".$fromDate."','".$toDate."')") or die(mysqli_error($con));
if(mysqli_num_rows($byData)>0)
{
if($type=='all')
{
?>
    <table class="table table-bordered table-striped table-condensed">
        <thead>
        <tr>
            <th>#</th>
            <th class="numeric">Type</th>
            <th class="numeric">Token</th>
            <th class="numeric" style="width:15%">Patient Name</th>
            <th class="numeric">Age</th>
            <th class="numeric">Gender</th>
            <th class="numeric" style="width:12%">Refferred By</th>
            <th class="numeric" style="width:15%">Reff. By Address</th>
            <th class="numeric" style="width:13%">Purpose of visit</th>
            <th class="numeric">Amount</th>
            <th class="numeric" style="width:12%">Date</th>
            <th class="numeric">Time</th>
            <th class="numeric">Status</th>
        </tr>
        </thead>
<?php
while($byDataResult = mysqli_fetch_assoc($byData))
{
    $total = $total+$byDataResult['Amount'];

    ?>
    <tr>
        <td><?php echo $x; ?></td>
         <td><?php echo $byDataResult['ScanType']; ?></td>
        <td><?php echo $byDataResult['TokenYesterday']."/".$byDataResult['TokenToday'] ?></td>
        <td class="numeric"><?php echo $byDataResult['PatientName']; ?></td>
        <td class="numeric"><?php echo $byDataResult['Age']; ?></td>
        <td class="numeric"><?php echo $byDataResult['Sex']; ?></td>
        <td class="numeric"><?php echo $byDataResult['RefferredBy']; ?></td>
        <td class="numeric"><?php echo $byDataResult['ReffByAddress']; ?></td>
        <td class="numeric" style="width: 300px;"><?php echo substr($byDataResult['PurposeOfvisit'],0,100); ?></td>
        <td class="numeric"><?php echo number_format($byDataResult['Amount'],2); ; ?></td>
        <td class="numeric"><?php echo $byDataResult['Date']; ?></td>
        <td class="numeric"><?php echo $byDataResult['created_at']; ?></td>
        <td class="numeric" style="color: red;text-align: center;">
                                                <?php
                                                if( $byDataResult['Status']==2)
                                                {
                                                    echo 'Edited';
                                                }
                                                else{
                                                    echo "---";
                                                }

                                              ?></td>
    </tr>
    <?php
    $x++;
}
?>
        <tr class="totalRow">
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th style="text-align: right;">Total</th>
            <th><?php echo number_format($total,2); ?>/-</th>
            <th></th>
            <th></th>
            <th></th>

        </tr>
        </tbody>
    </table>
<?php
}
else{

    $x = 1;
$total = 0;
$grandTotal = '';

$byDataType = mysqli_query($con,"CALL reportByDateType('".$fromDate."','".$toDate."','".$type."')") or die(mysqli_error($con));
if(mysqli_num_rows($byDataType)>0){
    ?>
  <table class="table table-bordered table-striped table-condensed">
        <thead>
        <tr>
            <th>#</th>
            <th class="numeric">Type</th>
            <th class="numeric">Token</th>
            <th class="numeric" style="width:15%">Patient Name</th>
            <th class="numeric">Age</th>
            <th class="numeric">Gender</th>
            <th class="numeric" style="width:12%">Refferred By</th>
            <th class="numeric" style="width:15%">Reff. By Address</th>
            <th class="numeric" style="width:13%">Purpose of visit</th>
            <th class="numeric">Amount</th>
            <th class="numeric" style="width:12%">Date</th>
            <th class="numeric">Time</th>
            <th class="numeric">Status</th>
        </tr>
        </thead>
<?php
while($byDataResults = mysqli_fetch_assoc($byDataType))
{
    $total = $total+$byDataResults['Amount'];

    ?>
    <tr>
        <td><?php echo $x; ?></td>
         <td><?php echo $byDataResults['ScanType']; ?></td>
        <td><?php echo $byDataResults['TokenYesterday']."/".$byDataResults['TokenToday'] ?></td>
        <td class="numeric"><?php echo $byDataResults['PatientName']; ?></td>
        <td class="numeric"><?php echo $byDataResults['Age']; ?></td>
        <td class="numeric"><?php echo $byDataResults['Sex']; ?></td>
        <td class="numeric"><?php echo $byDataResults['RefferredBy']; ?></td>
        <td class="numeric"><?php echo $byDataResults['ReffByAddress']; ?></td>
        <td class="numeric"><?php echo substr($byDataResults['PurposeOfvisit'],0,100); ?></td>
        <td class="numeric"><?php echo number_format($byDataResults['Amount'],2); ; ?></td>
        <td class="numeric"><?php echo $byDataResults['Date']; ?></td>
        <td class="numeric"><?php echo $byDataResults['created_at']; ?></td>
        <td class="numeric" style="color: red;text-align: center;">
        <?php
        if( $byDataResults['Status']==2)
        {
            echo 'Edited';
        }
        else{
            echo "---";
        }

      ?></td>
    </tr>
    <?php
    $x++;
}
?>
        <tr class="totalRow">
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th style="text-align: right;">Total</th>
            <th><?php echo number_format($total,2); ?>/-</th>
            <th></th>
            <th></th>
            <th></th>

        </tr>
        </tbody>
    </table>


    <?php
    }
    else{
        ?>
         <h3 style="text-align: center">Sorry! No Record Found</h3>
        <?php
    }
}
}
else{
 ?>
    <h3 style="text-align: center">Sorry! No Record Found</h3>
<?php
}
    ?>

希望它能帮助你:)

琴献
2023-03-14

您可以在发送ajax之前使用empty方法

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",
        beforeSend: $('#project_data').empty(),
        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);
            }
           }
      });

    });
}
 类似资料:
  • 我需要以编程方式将打印机添加到安装了System V过滤器的杯子中。现在我正在使用以下代码创建添加打印机的请求: 为了对话: 这似乎正确地将打印机添加到杯子系统,因为我可以使用以下命令将打印发送到它: 我的第一个想法是将我想用作过滤器的文件复制到目录中,并将其命名为myptr。我这样做了,给了它正确的用户、组和权限,但它似乎不起作用。我甚至试着在脚本的前面粘贴一个sleep 60,但它从未出现在p

  • 通过,我可以轻松地在方法中向添加一个筛选器。 如何使用添加筛选器?我必须使用XML吗? 为了帮助其他人更容易地理解Spring Web配置,我绘制了下面的插图。 比起记住那么多东西,查它还不那么痛苦。 和一篇关于Spring Web初始化的好文章: http://www.kubrynski.com/2014/01/commanding-spring-web-initialization.html

  • 问题内容: 我将jetty嵌入到我的应用程序中,并尝试找出如何添加servlet过滤器(用于cookie处理)。Wiki和javadoc的含义不明确,我缺少什么: 我在此找到的唯一信息是一个论坛帖子,建议对此文档进行改进。 问题答案: 更新:对于Jetty版本9.2.2: 原始答案=== 如果您不想使用web.xml,请使用以下命令: 如果确实要使用web.xml而不是addFilter()方法,

  • 我想知道,在spring boot中是否有类(用于web应用程序)的注释?也许是? 我想在我的项目中添加一个自定义筛选器。 spring boot参考指南提到了关于,但我不确定如何使用它。

  • 问题内容: 因此,我有一个数据数组,并且正在使用该数据生成组件列表。我想在每个生成的元素上都有一个引用来计算高度。我知道如何使用Class组件执行此操作,但是我想使用React Hooks进行操作。 这是一个说明我要做什么的示例: 问题答案: 不确定我是否完全理解您的意图,但我认为您需要这样的东西:

  • 问题内容: 我想知道,中的类(对于Web应用程序)是否有任何注释?也许@Filter吧? 我想在项目中添加自定义过滤器。 《 Spring Boot参考指南 》提到了关于 ,但是我不确定如何使用它。 问题答案: 如果您要设置第三方过滤器,则可以使用FilterRegistrationBean。例如相当于web.xml 这些将是@Configuration文件中的两个bean 上面已经用spring