Bring PHP id in Jqueryui autocomplete query

Asked

Viewed 258 times

0

I have this structure:

$(function($) {
  $( "#cliente" ).autocomplete({
    source: '../php/search_clientes.php'
  });
});
<div class="form-group">
  <div class="row">
    <div class="col-sm-12">
      <input type="text" value="" name="cliente" id="cliente" tabindex="1" class="form-control" placeholder="Pesquisar">
    </div>
  </div>
</div> 

Where the "search_clients.php" is:

<?php
	/******* Conexão com o bando de dados *******/
	include "../../Conexao/config.php";
	mysqli_select_db($config, $database_config);
	mysqli_set_charset($config,"utf8");
	/******* Conexão com o bando de dados *******/

	//get search term
	$searchTerm = $_GET['term'];

	$sql_1 = mysqli_query($config, "SELECT * FROM tb_agenda WHERE nome_razao LIKE '%".$searchTerm."%' AND tipo_contato = '12' ORDER BY nome_razao ASC LIMIT 10") or die(mysqli_error($config));

	if(@mysqli_num_rows($sql_1) <= '0'){
		//echo "$erro";	
	}else{
		while($r_sql_1 = mysqli_fetch_array($sql_1)){
			$cliente = $r_sql_1['nome_razao'];
			$data[]  = $cliente;
		}
	}

	//return json data
	echo json_encode($data);
?>

It is possible, some way to bring the id in some Hidden input, or bring the PHP id in some way?

I found this, but after a day of trying, I assume I need help.

2 answers

0


$( "#cliente" ).autocomplete({
    minLength: 2,
    source: function( request, response ) {
        /*esta requisição ira consultar  somente o nome do cliente */
        $.ajax({
            url: '../php/search_clientes.php',
            dataType: "json",
            data: {
                parametro: /*SERIA BOM ENVIAR UM NOVO PARAMENTRO PARA ESPECIFICAR QUER ESTA CONSULTANDO SOMENTE O NOME*/
                term: $('#cliente').val()
            },
            success: function(data) {
               response(data);
            }
        });
    },
    focus: function( event, ui ) {
        $("#cliente").val( ui.item.nome );
        carregarDados();//na função CARREGADADOS() é que você irá pegar a ID em uma nova  requisição
        return false;
    },
    select: function( event, ui ) {
        $("#cliente").val( ui.item.nome );
        return false;
    }
})
.autocomplete( "instance" )._renderItem = function( ul, item ) {
  return $( "<li>" )
    .append( "<a><b>Nome: </b>" + item.nome )
    .appendTo( ul );
};

function carregarDados(){
    var busca = $('#cliente').val();
    if(busca != "" && busca.length >= 3){
        $.ajax({
            url: '../php/search_clientes.php',
            dataType: "json",
            data: {
                acao: 'consulta',
                parametro: $('#cliente').val()
            },
            success: function( data ) {
               $('#INPUT_HIDDEN').val(data);//AQUI VOCE PODE ARMAZENAR O RETORNO EM QUALQUER LUGAR OU VARIAVEL
            }
        });
    }
}
  • I didn’t do the PHP part, but you’ll have to add another function just to query the ID and return Once the client name is selected.

0

Thanks an hour.

In PHP, I can do:

<?php
	/******* Conexão com o bando de dados *******/
	include "../../Conexao/config.php";
	mysqli_select_db($config, $database_config);
	mysqli_set_charset($config,"utf8");
	/******* Conexão com o bando de dados *******/

	//get search term
	$searchTerm = $_GET['term'];

	$sql_1 = mysqli_query($config, "SELECT * FROM tb_agenda WHERE nome_razao LIKE '%".$searchTerm."%' AND tipo_contato = '12' ORDER BY nome_razao ASC LIMIT 10") or die(mysqli_error($config));

	if(@mysqli_num_rows($sql_1) <= '0'){
		//echo "$erro";	
	}else{
		while($r_sql_1 = mysqli_fetch_array($sql_1)){
			$id_cliente = $r_sql_1['id'];
      $cliente    = $r_sql_1['nome_razao'];
			$data[]     = $id_cliente . " - " . $cliente;
		}
	}

	//return json data
	echo json_encode($data);
?>

In that I have a variable with the id.

But in Jquery, "parameter: /IT WOULD BE GOOD TO SEND A NEW PARAMETER TO SPECIFY EITHER BY CONSULTING ONLY THE NAME/". This new meter is the ID?

Sorry for the ignorance, but really I had never touched the advanced of the autocomplete.

Browser other questions tagged

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