1

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
  • 2
    in addition to Zakarias answer, i think you never call the function buscaDados Commented Dec 7, 2016 at 10:53
  • Where you are sending the data using echo? Commented Dec 7, 2016 at 11:51

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer @Zakaria, but how can I use the response on my actual variables? also if i am returning json data, do i need to set the type of data at my ajax code?
You're welcome, you could parse the data to json in the success callback, check my update.
1

in your ajax code u add a success.

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

    //You do not need to use  $.parseJSON(data).  You can immediately process data as array.
    console.log(data)

    //if you have a array you use the following loop

     for (var j =0;j < data.length;j++) {

    console.log(data[j]);
    // u can use data[j] and write to any fields u want.
    // e.g.
    $('.somediv').html(data[j].myarraykey);



     }

  })
  .done(function(resposta){
    console.log(resposta);
  })
  .always(function(){
    $(".mask-loading").fadeToggle(1000);
  })
});

And for the php codes (i did not check whether your code is valid or not), you need to add the echo and a die to end the call.

$rede = $_POST['rede'];

    $dados = array();

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

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

9 Comments

it return 'null' to my console @dave teu
can u please add dataType: 'JSON' too and for php code should be echo json_encode not son_encode (silly autocorrect)
if it still returns null that means your console.log is working properly, and so is the success call. It's likely your SQL code is returning null results.
@Dave, i tried your code, but it returns this error postimg.org/image/oggrwrqfx
Please remove .done codes and .always codes. U can put whatever you want in the success function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.