1
I have a system where the administrator can approve or disapprove the user, It is all ready, but I want to know how to do this process with ajax, so that every time I approve or disapprove a user the page does not give refresh
.
Approval page
<div class="row">
<div class="col-md-5"><h3>Autorização de Usuarios</h3></div>
</div>
<div class="row alinha-tabela">
<div class="row">
<div class="col-md-6">
<div class="table-responsive shadow-z-1">
<table class="table table-striped table-condensed table-hover">
<thead>
<tr>
<th>Codigo</th>
<th>Usuario</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
//Consulta
$buscarusuario=$pdo->prepare("SELECT * FROM usuario");
$buscarusuario->execute();
//atribuindo dados á variavel
$linha = $buscarusuario->fetchAll(PDO::FETCH_ASSOC);
//percorrendo a variavel para listar os dados
foreach ($linha as $listar) {
$iduser = $listar['id'];
echo "<tr>";
echo " <td>".$listar['id']."</td>";
echo "<td>".$listar['nome']."</td>";
if($listar['status'] > 0 ){
echo "<td class='success text-success'>Aprovado
<form method='post' action='pg/mudastatus.php' id='f-desaprova' class='form-muda'>
<button type='submit' class='btn btn-xs btn-success alinha-btn' name='desaprova' value='$iduser'>Desaprovar</button>
</form>
</td>";
}else{
echo "<td class='danger text-danger'> Aguardando aprovação
<form method='post' action='pg/mudastatus.php' id='f-aprova' class='form-muda'>
<button type='submit' class='btn btn-xs btn-danger alinha-btn' name='aprova' value='$iduser' >Aprovar</button>
</form>
</td>";
}
}
?>
</tbody>
</table>
</div>
</div>
mudastatus.php
<?php
include '../config/config.inc.php';
if(isset($_POST['aprova'])){
$atualizarstatus = $pdo->prepare("UPDATE usuario SET status=1 WHERE id=:ID ");
$atualizarstatus->bindValue(":ID",$_POST["aprova"]);
$atualizarstatus->execute();
$linha = $atualizarstatus->rowCount();
if($linha > 0){
header("location:../logado.php");
}else{
echo "Erro ao Mudar status";
}
}elseif (isset($_POST['desaprova'])){
$atualizarstatus = $pdo->prepare("UPDATE usuario SET status=0 WHERE id=:ID ");
$atualizarstatus->bindValue(":ID",$_POST["desaprova"]);
$atualizarstatus->execute();
$linha = $atualizarstatus->rowCount();
if($linha > 0){
header("location:../logado.php");
}else{
echo "Erro ao Mudar status";
header("location:../logado.php");
}
}
hello friend could explain me more detailed how would be my code with ajax and let cleaner without two form ?
– Thalles Honorato