I cannot show database data with ajax,php and jquery

Asked

Viewed 44 times

0

I need you to click on checkbox, he makes a database query and returns it to me inside a div. But I can pull the data with ajax and show in div directly(home_.php is showing the select I did), but when joining the ajax click event, I can’t get anything.

-By clicking on the checkbox, it will consult the BD,and show me the result in the div, will be flexbox.

HTML

<div id="conteudo">
                RESULTADO:
            </div>
<div class="checkboxes">

                        <input type="checkbox" id="regua">
                        <label for="regua">Régua</label><br>
                        <input type="checkbox" id="motor">
                        <label for="motor">Motor</label> <br>
                        <input type="checkbox" id="canal">
                        <label for="canal">Canal</label><br>
                        <input type="checkbox" id="fase">
                        <label for="fases">Fase</label><br>
                        <input type="checkbox" id="vigencia">
                        <label for="vigencia">Vigência</label>

                    </div>

JQUERY

<script src="jquery-3.4.1.js"></script>
            <script type="text/javascript">
                 $("#regua").click(function(){
                         if($('#regua').is(':checked')){

                           }
                     });

            </script>

AJAX

            <script>
                       $.ajax({
                                url: "home_.php",  // coloque aqui o endereço que vai buscar os dados no banco         
                                success: function (data) {                                  
                                    $('#conteudo').html(data); 
                                    $('#conteudo').show(); // Mostrar o retorno em texto no html

                                },
                                error: function (request, status, erro) {

                                    $('#conteudo').html('Ocorreu um erro, entre em contato com T.I!', erro);
                                    $('#conteudo').show();
                                }
                            });
            </script>   

home_.php

<?php
include_once("./php/conect_postgre.php");

  $consulta_bd = "SELECT * FROM uniao  WHERE regua LIKE 'Co%'";

  $result=pg_query($conexao, $consulta_bd);
  if  (($result)){
      while($linha_usuario = pg_fetch_assoc($result)){
          echo $linha_usuario['regua'] . "<br>";
      }
  }else{
      echo "Nenhum resultado encontrado";
  }
  • You have to put AJAX inside if($('#regua').is(':checked')){. Another thing, the script $("#regua").click(function(){ has to come at the end of the body.

  • I changed as said, and even clicking, does not return me the data, when ajax ta alone pulling, brings normally.

  • Put the script at the end of body?

  • yes put, it would be before the </body> right?

  • Yeah. Put a alert("ok"); inside the if to see if it is calling.

1 answer

1


   <script type="text/javascript">
                 $("#regua").click(function(){
                         if($('#regua').is(':checked')){
                            $.ajax({
                                url: "home_.php",  // coloque aqui o endereço que vai buscar os dados no banco         
                                success: function (data) {                                  
                                    $('#conteudo').html(data); 
                                    $('#conteudo').show(); // Mostrar o retorno em texto no html

                                      $.each(data, function (i, element) {
                                        $('#conteudo').html(element.regua); // não sei como ta o teu retorno
                                        $('#conteudo').html(element.linha_usuario); // ou assim
                                     });

                                },
                                error: function (request, status, erro) {

                                    $('#conteudo').html('Ocorreu um erro, entre em contato com T.I!', erro);
                                    $('#conteudo').show();
                                }
                            });
                           }
                     });

            </script>
  • 1

    MY GOD!!! YOU ARE 10 GIRL!!! YOU WORSHIP!!! FUNCIONOOOOOOOOOOOOOOOOOOU

  • Not at all... That’s the intention..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.