Read PHP data in Javascript

Asked

Viewed 357 times

3

I am a beginner programmer in both PHP and Javascript and I have a doubt in a project I am developing.

I have a server connected to a PHP file and am requesting the following fields

$FLD="idfca, solfca";

I created a function within Javascript to load my records, from 10 to 10, with scroll:

    var contador = 1;

$(document).ready(function () {
    carregar();
    $(this).on('scroll', function () {
        if ($(window).scrollTop() + $(window).height() >= $(this).height()) {
            carregar();
        }
    });
});

function carregar() {
    for (var i = 0; i < 10; i++) {
        $("#lista").append('<li><img src="http://placehold.it/100x100"/><div><h4>' + contador + ' Item</h4><p>Descrição do item '+contador+'</p></div></li>');
        contador++;
    }
}	

What I want to know is how to use the fields I’m pulling from the bank inside this javascript function, instead of the counter.

  • 2

    Friend, use the technology Ajax to get the data from PHP. And, in PHP, you answer this data as JSON. Just use the json_encode function :)

  • @Ajax wallacemaxters is more for technique not?

  • @durtto I know there, I’m confused until today, but I asked about it here.

  • http://answall.com/questions/116153/ajax-n%C3%A3o-%C3%A9-a-program-language%C3%A7%C3%A3o-ent%C3%A3o-o-que-%C3%A9

1 answer

2

A web application has 3 elements: client, server and persistence.

On the client side, Javascript is used in virtually all cases. On the server, PHP is used, for example. And in persistence any DBMS, like Mysql and Postgresql.

Javascript cannot talk directly to DBMS, but it can perform requests to the web server (implemented in PHP, for example) through Ajax (nothing more than HTTP requests triggered by a Javascript).

PHP in turn, through the drivers, can communicate with DBMS.

Therefore, what you will have to do is perform an Ajax request within your Javascript command carregar to your server. In this request, the pertinent information must be passed, such as which fields to return, range of returned results, etc. In PHP, in turn, you will interpret the request and send SQL commands to Mysql in order to generate the response to the request.

Once Mysql returns the data, it is best to format it as a Json through the function json_encode PHP and send this data in response to your Ajax request (something like echo json_encode($dados);). The advantage of this is that the Json will be interpreted by the browser and you will be able to access it as an object, facilitating its manipulation.

Finally, its function is carregar will have the necessary data to fill in the page, simply generate the elements and fill them with the information that has just been returned.

Note that there are several tasks to be performed. I presented, in general, what should happen so that you can obtain the desired data and present them to the customer. Now you must study the technologies cited, and apply them to the solution of your problem.

Browser other questions tagged

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