Searching Array of variables via Ajax and inserting in its due inputs a form

2

I have a form that searches and fills, via ajax, the person's name and year of birth (in their respective fields), all from the cpf entered by the user.

It's something like this:

Digite o CPF do usuário ______________  <button>BUSCAR DADOS</button>

NOME _________________________

DATA DE NSACIMENTO __________________

With this, two requests are made as follows:

$('#botao').click(function(){

        //BUSCA NOME
        $.get('buscar_nome.php?cpf='+$('#cpf').val(), 

        function(result) {
        $('#nome').val(result);
        });

        //BUSCA DATA DE NASCIMENTO
        $.get('buscar_data_nascimento.php?cpf='+$('#cpf').val(), 

        function(result) {
        $('#datanascimento').val(result);
        });

PHP pages have the following logic:

search_name.php

$cpf = $_GET['cpf'];
$sq1 = select * from tabela where cpf = '$cpf';
$registro = myslq_fetch_array($sq1);

echo registro['nome'];

search_data_name.php

$cpf = $_GET['cpf'];
$sq1 = select * from tabela where cpf = '$cpf';
$registro = myslq_fetch_array($sq1);

echo registro['datanascimento'];

It works perfectly, however, I would like to optimize the search for this data (name and date of birth) using only one request. I guess I should use Array. How should I proceed?

    
asked by anonymous 05.08.2016 / 01:13

1 answer

1

You can do something like this.

search.php

$cpf = $_GET['cpf'];
$sq1 = select nome,datanascimento from tabela where cpf = '$cpf';
$registro = myslq_fetch_array($sq1);

header('Content-Type: application/json');
echo json_encode($registro);

And in javascript access as follows

$.get('buscar.php?cpf='+$('#cpf').val(), 

function(result) {
   $('#nome').val(result.nome);
   $('#datanascimento').val(result.datanascimento);
});

Just be careful about this stretch

  

where cpf = '$ cpf'

It is possible to sql injection in this parameter. You should sanitize and validate the parameters that come from the user before putting it in a select this way.

    
05.08.2016 / 17:00