How to fetch values in a PHP file with jQuery - AJAX?

0

I want to get a string in a PHP file called search.php I want to return a string for my index.php but I want to do this when calling the try_it () function, is there any way I can do this with jQuery? I do not want index.php to update the page, only the value of the variable that will be returned by the search.php

index.php

<html>
    <head>
        <title>teste</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><!--LatestcompiledandminifiedCSS--><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
    <body>
        <button class="btn btn-default" id="executar" onclick="buscar()" type="button">Buscar</button>

        <script>
            var stringDoPHPvemPraCa;

            function buscar(){
                //quero que a string do php seja setada em stringDoPHPvemPraCa no momento em que eu chamar essa funcao
            }
        </script>
    </body>
</html>

search.php

include 'config.php';
include 'database.php';
include 'connection.php';

$pesquisa = DBSearch("usuarios","WHERE id = 1","questoes_resolvidas"); // isso me retorna um array

// com as questões resolvidas onde "usuarios" é a tabela "WHERE id = 1" 
// é o parametro e "questoes_resolvidas" é o campo a ser retornado (a string em si)
$pesquisa[0]['questoes_resolvidas'] //isso que eu quero no index.php
    
asked by anonymous 26.01.2017 / 18:14

1 answer

0
$.get('url', {parametro: valor}, function(data){
  console.log(data);
});

I believe the above example will solve.

link

After that, just manipulate as you see fit, for example:

Considering that your url that returns the search is link , your code would look something like this (obs: you will not had closed the head tag in your HTML):

<html>
    <head>
        <title>teste</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><!--LatestcompiledandminifiedCSS--><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
    </head>
    <body>
        <button class="btn btn-default" id="executar" onclick="buscar()" type="button">Buscar</button>

        <script>
            var stringDoPHPvemPraCa = '';
            function buscar(){
                $.get('http://localhost/buscar.php', function(data){
                    stringDoPHPvemPraCa = data;
                });
            }
        </script>
    </body>
</html>

Already your search.php would look like:

<?php

header('Content-Type: application/json');
include 'config.php';
include 'database.php';
include 'connection.php';

$pesquisa = DBSearch("usuarios","WHERE id = 1","questoes_resolvidas"); // isso me retorna um array

// com as questões resolvidas onde "usuarios" é a tabela "WHERE id = 1" 
// é o parametro e "questoes_resolvidas" é o campo a ser retornado (a string em si)
$resultado = $pesquisa[0]['questoes_resolvidas'] //isso que eu quero no index.php

echo json_encode($resultado);
exit;

?>
    
26.01.2017 / 18:17