How to put an echo json_encode passing a PHP function with javascript parameter?

Asked

Viewed 336 times

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.

  • 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.

  • And how I do it?

  • 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

1 answer

1


Hello,

Well I believe that the best way to do what you want to do is by creating a PHP API that will display on the screen only the JSON of information obtained from the database, creating this API you will be able to define GET parameters of url, such as a limit on how many items you will search for in the bank, so after you have made the API that plays only JSON on the screen, you go to your page where you want to display the information and use the Jquery AJAX to access your API and get the JSON, and how you set GET parameters in your API as the limit you will be able to pass in your AJAX when getting the information how much limit you want it to be.

I believe this is the best way. If you want I can show you some examples, but they are great, try to do first!

Browser other questions tagged

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