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

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

报告相同问题?

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题