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

Asked

Viewed 1,076 times

0

Guys I want to search a string in a PHP file called search.php I want to return a string to my index.php but I want to do this when calling the function try_it(), is there any way to do this with jQuery? I don’t 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>
        <!-- Latest compiled and minified CSS -->
        <link rel="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>

php search.

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
  • Post the codes so we can help

  • OK I’ll do it, just a moment

  • Take a look in this tutorial. The list you simply upload in the foreach of your bank, doing a like.

1 answer

1

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

I believe the above example solves.

https://api.jquery.com/jquery.get/

After that it is only to manipulate in the way you want, for example:

Taking into account that your url that returns the search is http://localhost/search.php, your code would look something like this (Obs: vc had not closed the head tag on 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>
        <!-- Latest compiled and minified CSS -->
        <link rel="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 be:

<?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;

?>
  • how I would apply that to the case where I posted my code?

  • first, you should echo at the end of your php: echo $pesquisa[0]['questoes_resolvidas'] $.get to take the value and move to the variable: $.get('caminho/do/arquivo.php', function(data){ &#xA; stringDoPHPvemPraCa = data;&#xA;}); Once you execute the method, it will put the value in the variable. (bad comment does not skip line).

Browser other questions tagged

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