Guest User

Untitled

a guest
Dec 6th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. if (isset($_GET["txtnome"]))
  2. {
  3. $nome = $_GET["txtnome"];
  4.  
  5.  
  6. // Conexao com o banco de dados
  7. $host = "127.0.0.1";
  8. $port = "5432";
  9. $db = "agenda";
  10. $user = "postgres";
  11. $pass = "postgres";
  12.  
  13. $conexao = new PDO("pgsql:host=$host;dbname=$db;user=$user;password=$pass") or die("Erro na conexão!");
  14.  
  15.  
  16. // Verifica se a variável está vazia
  17. if (empty($nome)) {
  18. $sql = "SELECT * FROM contato";
  19. } else {
  20. $nome .= "%";
  21. $sql = "SELECT * FROM contato WHERE nome like '$nome'";
  22. }
  23. sleep(1);
  24. $result = $conexao->query($sql) or die("Erro na consulta!");
  25. $contact = $result->fetchAll();
  26.  
  27. // Verifica se a consulta retornou linhas
  28. if (!empty($contact)) {
  29. // Atribui o código HTML para montar uma tabela
  30. $tabela =
  31. "
  32. <div class='row'>
  33. <div class='col-md-6'>
  34.  
  35. <table border='1' class='table table-bordered table-hover table-striped'>
  36. <thead class='bg-info'>
  37. <tr>
  38. <th>NOME</th>
  39. <th>TELEFONE</th>
  40. <th>CELULAR</th>
  41. <th>EMAIL</th>
  42. <th>ID</th>
  43. <th>OPÇÃO</th>
  44. </tr>
  45. </thead>
  46. <tbody>
  47. <tr id='hidden'>";
  48. $return = "$tabela";
  49.  
  50. // Captura os dados da consulta e insere na tabela HTML
  51.  
  52. foreach ($contact as $row) {
  53. $return.= "<td>" . utf8_encode($row["nome"]) . "</td>";
  54. $return.= "<td>" . utf8_encode($row["telefone"]) . "</td>";
  55. $return.= "<td>" . utf8_encode($row["celular"]) . "</td>";
  56. $return.= "<td>" . utf8_encode($row["email"]) . "</td>";
  57. $return.= "<td id='deletar'>" . utf8_encode($row["id"]) . "</td>";
  58. $return.= "<td>" . "<input type='button' class='btn btn-danger btn-sm' value='Excluir' onclick='excluir();'/>" . "</td>";
  59. $return.= "</tr>";
  60. }
  61. echo $return.="</tbody></table></div></div>";
  62. } else {
  63. // Se a consulta não retornar nenhum valor, exibe mensagem para o usuário
  64. echo "Não foram encontrados registros!";
  65. }
  66. }
  67.  
  68. function excluir(){
  69.  
  70. swal({
  71. title: 'Tem certeza que deseja excluir este contato?',
  72. text: "Você não poderá reverter isso!",
  73. type: 'warning',
  74. showCancelButton: true,
  75. confirmButtonColor: '#3085d6',
  76. cancelButtonColor: '#d33',
  77. confirmButtonText: 'Sim, exclua!'
  78. }).then((result) => {
  79. if (result.value) {
  80.  
  81. var elemento = document.getElementById("deletar").innerHTML;
  82. document.getElementById("deletar").value = elemento;
  83.  
  84. var id = document.getElementById("deletar").value;
  85. var resultado = document.getElementById("result");
  86.  
  87. var xmlreq = CriaRequest();
  88.  
  89. xmlreq.open("GET", "contato.php?deletar=" + id, true);
  90.  
  91. // Atribui uma função para ser executada sempre que houver uma mudança de ado
  92. xmlreq.onreadystatechange = function(){
  93.  
  94. // Verifica se foi concluído com sucesso e a conexão fechada (readyState=4)
  95. if (xmlreq.readyState == 4) {
  96. // Verifica se o arquivo foi encontrado com sucesso
  97. if (xmlreq.status == 200) {
  98. resultado.innerHTML = xmlreq.responseText;
  99. document.getElementById("hidden").style.display = "none";
  100. swal(
  101. 'Deleted!',
  102. 'Your file has been deleted.',
  103. 'success'
  104. )
  105.  
  106. }else{
  107. resultado.innerHTML = "Erro: " + xmlreq.statusText;
  108. }
  109. }
  110. };
  111. xmlreq.send(null);
  112. } //if do result.value
  113. }) //swal principal
  114. } //funcao excluir
  115.  
  116. if (isset($_GET["deletar"])) {
  117.  
  118. $id = $_GET["deletar"];
  119.  
  120. // Conexao com o banco de dados
  121. $host = "127.0.0.1";
  122. $port = "5432";
  123. $db = "agenda";
  124. $user = "postgres";
  125. $pass = "postgres";
  126.  
  127. $conexao = new PDO("pgsql:host=$host;dbname=$db;user=$user;password=$pass") or die("Erro na conexão!");
  128.  
  129. $result = $conexao->prepare("DELETE FROM contato WHERE id = :deletar");
  130. $result->bindValue(':deletar', $id);
  131.  
  132. sleep(1);
  133.  
  134. if (!($result->execute())) {
  135. echo "Erro ao excluir contato :(";
  136. }
  137. }
Add Comment
Please, Sign In to add comment