How to do something asynchronous with jQuery, search in MySQL with PHP?

1

I'm having a lot of trouble with this part. I want to simply do a search in the database and move to my main program, this without giving a refresh . I know how to do all of PHP and MySQL, however the jQuery part is no longer my area, I know the basics of JS.

Next: The search I want is SELECT questoes_resolvidas FROM dados_usuario WHERE id = id_usuario; . This I know how to do. But the big problem is to get this result and send it to my index.php . I do not want to send the result into a variable in index.php . It can be a variable of the same JavaScript itself.

I have tried to pass $_SESSION but it does not work, because I have to update the page to $_SESSION to get the updated values. I do not want to refresh the page, but the variable with the MySQL command response will be updated every time I call the try_it() function.

Help me, please. I've tried to learn this jQuery but I think I need to go deeper into JS first, and I do not have time for that right away.

    
asked by anonymous 26.01.2017 / 17:30

3 answers

0

After a long time (I could not remember the question again) I found some Google searches. It's quite simple:

$.ajax({
    type: "GET",//Tipo da requisicao, pode ser GET, POST, etc...
    data: {dado1: "Dado 1",dado2: "Dado 2"},
    url: "url_do_arquivo_que_faz_as_coisas.php",
    beforeSend: function(){
        console.log("Isso executara antes da requisicao");
    }
    success: function(responses){
        console.log(responses);//Aqui são os dados vindos do PHP
    }
});

If it was not in PHP we would have something like this

echo $_GET['dado1'].' - '.$_GET['dado2'];

We would have the following in our console after making the AJAX request:

Dado 1 -Dado 2

I hope I have helped those who need it like I needed then. In addition, you should put the JS code in a function, so you can call when you want and as many times as you like, the rest and only modify ...

    
30.09.2018 / 23:24
0

You can create a webservice to provide this data. Basically a file that will return only this data.

First create a file, ajax.php, for example. In it you make the query and write the result. To make it easier for you, I recommend that you print the parsed data in JSON.

In addition, in jquery you use the Ajax method $.ajax(options, callback)

Asynchronously it makes a request in the URL that you pass and when finished calls the callback. When you fall into the callback you treat the data you received, or a possible crash

    
26.01.2017 / 17:44
0

You will have to have a page to receive this ajax request.

I explained below, the connection to the bank is fictitious, just to illustrate.

ajax.php

// Apenas para exemplificar uma conexão com o DB
include("conexao_db.php");

$sql = <<<SQL
    SELECT *
    FROM minha_tabela
    WHERE minha_condicao = 1;
SQL;

$resultado = $db->query($sql);
$dados = $resultado->fetchAll();
// - - - - - - - - - - - - - - 

// Parte importante
header("content-type: application/json");
echo json_encode($dados);

Then in your HTML you will make an AJAX request for the newly created page and include it in the current page.

index.html

<h3>Meus dados</h3>

<ul id="minha_lista"></ul>

<script type="text/javascript">
    // Faz um requisição para a página "ajax.php", recebe os dados do servidor
    // e adiciona na lista vazia acima
    $.getJSON( "ajax.php", function( data ) {
        var items = [];

        $.each(data, function( key, val ) {
            items.push( "<li name='" + key + "'>" + val + "</li>" );
        });

        $('#minha_lista').html(items.join(''));
    });
</script>
    
26.01.2017 / 18:28