0
My question is this::
I have a php class that has a function that searches for an array of data and I put an echo json_encode that will then make the autocomplete work.
as follows:
<script>
$(document).ready(function()
{
$.ajaxSetup({ cache: false });
$('#search').keyup(function()
{
var searchField = $('#search').val();
var a = $('#search').val();
if (!searchField){
return;
}else
{
var expression = new RegExp(searchField, "i");
$.each(<?php echo json_encode($a->buscarArtistasBuscaPrincipal());?>, function(key, value)
{
if (value.nome.search(expression))
{
$('#result').append('....');
}
});
}
});
});
</script>
Works normally! However I want to do different, I want to search not all table data but only words that correspond with what the person type, putting for example her in the class a select more so less:
SELECT nome_artista, img_artista FROM artista where nome_artista like "."'%".$busca.
Instead of this variable $search will be passed the value that is inside the variable javascript searchField, which is what the person typed.
How do I pass this searchField value to $search variable?
I tried to do that:
$.each(<?php echo json_encode($a->buscarArtistasBuscaPrincipal("<script>document.write(searchField)</script>"));?>, function(key, value)
But it does not work, I know that PHP runs on the server and javascript does not, but there must be a solution for this. If anyone can help me I really appreciate!
It would be interesting if you used ajax to perform the requests for each change in input field. doing so with every time a letter was typed, a select was made in the bank.
– Vinicius De Jesus
It’s like Vinicius said above. The way you are doing it has no way to change PHP because it has already returned the results.
– Sam
And how I do it?
– Elisa
Take a look at this, and similar to how you will solve your problem. https://answall.com/questions/180131/como-fazer-algo-ass%C3%Adncrono-com-jquery-busca-no-mysql-com-php/333305#333305
– Daniel Santos