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

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.

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