Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Giihh

CRUD PDO com Ajax (delete checkbox)

Recommended Posts

Olá,

 

CRUD esta funcionando perfeitamente, em PDO, mas a cada ação atualiza a página, então agora com ajax quero que de o refesh apenas onde esta sendo alterado. Primeiramente estou tentando alterar os deletes, onde será possível selecionar 1 checkbox ou varios para deletar de uma vez os ids selecionados, com return false não fica bom, senão se o usuário esquecer de apagar um outro registro e for tentar apagar não vai conseguir. É a primeira vez que uso Ajax, onde estou errando?

 

unidades.php

<div id="alterarunidades">

<table class="table table-hover">

    <thead>
        <tr>
            <th>#</th>
            <th>Nome da unidade:</th>
            <th>Endereço</th>
            <th>Cidade</th>
            <th>Contato</th>
        </tr>
    </thead>

    <tbody>
        <?php
        $sqlRead = 'SELECT * FROM unidades ORDER BY id DESC';
        try {
            $read = $db->prepare($sqlRead);
            $read->execute();
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
        while ($rs = $read->fetch(PDO::FETCH_OBJ)) {
            ?>
            <tr>
                <td><?php echo $rs->id; ?></td>
                <td><?php echo $rs->nomeunidade; ?></td>
                <td><?php echo $rs->endunidade; ?></td>
                <td><?php echo $rs->cidadeunidade; ?></td>
                <td><?php echo $rs->contatounidade; ?></td>
                <td>
                    <a href="index.php?action=update&id=<?php echo $rs->id; ?>"><span class="glyphicon glyphicon-refresh" style="color: #00578a;"></span></a>
                    <input type="checkbox" id="excluir_unidade" value="<?php echo $rs->id; ?>">
                   
                </td>
            </tr>
        <?php } ?>
    </tbody>

</table>
 </div>
<button type="submit" onclic="alterar_div_unidades()" class="btn btn-primary" style="float:right">Excluir Selecionados</button>   


<script type="text/javascript">
function alterar_div_unidades() {
  $.ajax({
    type: "POST",
    url: "http://localhost:8080/notecel/unidades/processa_alterar_unidades.php",
    data: {
             excluir_unidade: $('#excluir_unidade')        
    },
    success: function(data) {
      $('#alterarunidades').html(data);
    }
  });
}    
</script>    

processa_alterar_unidades.php

<?Php
if(isset($_GET['id']) && $_GET['id'] == 'alterar_div_unidades()'){
$id = (int)$_GET['id'];

$sqlDelete = 'DELETE FROM unidades WHERE id = :id';

try {
$delete = $db->prepare($sqlDelete);
$delete->bindValue(':id', $id, PDO::PARAM_INT);
if($delete->execute()){
echo "<div class='alert alert-success'>
    <button type='button' class='close' data-dismiss='alert'>×</button>
    <strong>Deletado com sucesso!</strong>
</div>";
}
} catch (PDOException $e) {
echo "<div class='alert alert-error'>
    <button type='button' class='close' data-dismiss='alert'>×</button>
    <strong>Erro ao deletar dados!</strong>" . $e->getMessage() . "
</div>";
}
}
?>

<?php

echo "
<table class='table table-hover'>

    <thead>
        <tr>
            <th>#</th>
            <th>Nome da unidade:</th>
            <th>Endereço</th>
            <th>Cidade</th>
            <th>Contato</th>
        </tr>
    </thead>

    <tbody>
     ";
        $sqlRead = 'SELECT * FROM unidades ORDER BY id DESC';
        try {
            $read = $db->prepare($sqlRead);
            $read->execute();
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
        while ($rs = $read->fetch(PDO::FETCH_OBJ)) {
            echo"
            <tr>
                <td>".$rs->id."</td>
                <td>".$rs->nomeunidade."</td>
                <td>".$rs->endunidade."</td>
                <td>".$rs->cidadeunidade."</td>
                <td>".$rs->contatounidade."</td>
                <td>
                    <a href='index.php?action=update&id='".$rs->id."'><span class='glyphicon glyphicon-refresh' style='color: #00578a;'></span></a> 
                    <input type='checkbox' onclick='alterar_div_unidades()' value='".$rs->id."'>
                    
                </td>
            </tr>";}

    echo "    </tbody>

</table>
";
 

Compartilhar este post


Link para o post
Compartilhar em outros sites
<?Php
if(isset($_GET['id']) && $_GET['id'] == 'alterar_div_unidades()'){
$id = (int)$_GET['id'];

$sqlDelete = 'DELETE FROM unidades WHERE id = :id';

try {
$delete = $db->prepare($sqlDelete);
$delete->bindValue(':id', $id, PDO::PARAM_INT);
if($delete->execute()){
echo "<div class='alert alert-success'>
    <button type='button' class='close' data-dismiss='alert'>×</button>
    <strong>Deletado com sucesso!</strong>
</div>";
}
} catch (PDOException $e) {
echo "<div class='alert alert-error'>
    <button type='button' class='close' data-dismiss='alert'>×</button>
    <strong>Erro ao deletar dados!</strong>" . $e->getMessage() . "
</div>";
}
}
?>

<?php

echo "
<table class='table table-hover'>

    <thead>
        <tr>
            <th>#</th>
            <th>Nome da unidade:</th>
            <th>Endereço</th>
            <th>Cidade</th>
            <th>Contato</th>
        </tr>
    </thead>

    <tbody>
     ";
        $sqlRead = 'SELECT * FROM unidades ORDER BY id DESC';
        try {
            $read = $db->prepare($sqlRead);
            $read->execute();
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
        while ($rs = $read->fetch(PDO::FETCH_OBJ)) {
            echo"
            <tr>
                <td>".$rs->id."</td>
                <td>".$rs->nomeunidade."</td>
                <td>".$rs->endunidade."</td>
                <td>".$rs->cidadeunidade."</td>
                <td>".$rs->contatounidade."</td>
                <td>
                    <a href='index.php?action=update&id='".$rs->id."'><span class='glyphicon glyphicon-refresh' style='color: #00578a;'></span></a> 
                    <input type='checkbox' onclick='alterar_div_unidades()' value='".$rs->id."'>
                    
                </td>
            </tr>";}

    echo "    </tbody>

</table>
";
 

:cry: Meu Deus

 

http://www.upinside.com.br/curso/curso-pro-jquery

 

Off

 

Tem mandei uma mensagem privada

 

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.