普通网友 2016-12-07 10:48
浏览 29

返回数据AJAX PHP

By defaut, when my system loads some data is filtered in my db and shown to the user. But my doubt is how can I call AJAX to filter some new data, and return it, changing the default values that are already set on my variables.

This is my AJAX call:

 $("#botao-filtrar").click(function(){
  $(".mask-loading").fadeToggle(1000);
  $.ajax({
    url: 'datacenter/functions/filtraDashboardGeral.php',
    type: 'POST',
    data: {rede: $("#dropdown-parceria").val()},
  })
  .done(function(resposta){
    console.log(resposta);
  })
  .always(function(){
    $(".mask-loading").fadeToggle(1000);
  })
});

And this is what I got from trying to filter some data to return it, but nothing worked:

<?php
    require_once('../../includes/conecta.php');

    $rede = $_POST['rede'];

    function buscaDados($conexao){
        $dados = array();

        $resultado = mysqli_query($conexao, "SELECT * FROM evolucao_originacao WHERE rede = {$rede}");

        while($valores = mysqli_fetch_assoc($resultado)){
            array_push($dados, $valores);
        }
    }

Any idea?

Thanks!

  • 写回答

2条回答 默认 最新

  • weixin_33749131 2016-12-07 10:51
    关注

    You should add echo at the end :

    echo json_encode($dados);
    

    So the $dados array will be sent back to the ajax request as JSON response.

    Parse the response to json uisng $.parseJSON() :

    .done(function(resposta){
         resposta = $.parseJSON(resposta);
    
         console.log(resposta);
    })
    

    Hope this helps.

    评论

报告相同问题?