dongqiu9018 2018-02-19 00:07
浏览 44
已采纳

PHP:单击按钮更新一个具有不同SELECT语句的表?

I have a table on my page and the first time it is loaded is when the user enters the page. On my database table I have a BIT and on my page I have two buttons. When I click in the first button I want to show in the table the data with the bit in 0 and in the second button, the data with the bit in 1. I tried creating a function:

$EstadoAtivo = 0;
$EstadoEncerrado = 1;
function updateTable() {
    $connect = mysqli_connect("localhost", "user", "password", "dbname")

    if($showFirst){
        $query = "SELECT * FROM tabela_ordens WHERE Estado = '$EstadoAtivo' ORDER BY Id desc";
    }else{
        $query = "SELECT * FROM tabela_ordens WHERE Estado = '$EstadoEncerrado' ORDER BY Id desc";
    }
    $result = mysqli_query($connect, $query);
}

And then when I click the first button I run:

<?php 
$showFirst = true;
updateTable();
?> 
$("#table-ordens").load('index #table-ordens');

and the second button is the same but $showFirst = false;

And inside my table I have:

<?php 
while($row = mysqli_fetch_array($result))
{
?>
<tr>
  <td><?php echo $row['ID']; ?></td>
  <td><?php echo $row['Cliente']; ?></td>
  <td><?php echo $row['Equipamento']; ?> </td>
  <td><input type="button" name="view" value="Exibir" id="<?php echo 
  $row['ID'];?>" class="btn mostrar_info btn-block" />
  </td>                                                  
  <td><input type="button" name="view" value="Encerrar" id="<?php echo $row['ID'];?>" class="btn encerrar_chamado btn-block btn-danger" /></td> </tr>
 <?php  
  }  
 ?>  

But it doesn't work, nothing changes when I click my buttons.

  • 写回答

1条回答 默认 最新

  • dongxing7318 2018-02-19 00:22
    关注

    At First, You make your Query inside a specific element, And lets say a div

    <div id='posts'>
      <?php 
      $query = $query = "SELECT * FROM tabela_ordens WHERE Estado = '$EstadoAtivo' ORDER BY Id desc";
      execute $query . . .
      echo values . . .  
      ?>
    </div>
    <button id='update'></button>
    

    make your ajax.php page with the other query

    <?php 
      $query = "SELECT * FROM tabela_ordens WHERE Estado = '$EstadoEncerrado' ORDER BY Id desc";
      execute $query . . .
      echo values . . .  
    ?>
    

    Then you make your AJAX function and call on with $(document).on

    function update() {
        $.get(. .ajax.php. . . success: $("#posts").load(data); . );
    }
    
    $(document).on('click', '#update', function(){
        update();
    })
    

    Keep in mind .load() will append the new values, If you want to remove all the previous values from the html and put the new ones, use .html()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?