Read the PHP data in Javascript

3

I'm a beginner programmer both in PHP and Javascript and I'm having a question about a project I'm developing.

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

$FLD="idfca, solfca";

I created a function inside Javascript to load my records, 10 out of 10, with the 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 function in javascript instead of the counter.

    
asked by anonymous 20.02.2015 / 14:43

1 answer

2

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

On the client side, Javascript is used in almost all cases. On the server, PHP is used, for example. And in persistence any DBMS, such as MySQL and PostgreSQL.

Javascript can not talk directly to the DBMS, but it can execute 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 drivers , can communicate with the DBMS.

So, what you will have to do is perform an Ajax request within your% 2% JavaScript command for your server. In this request, 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 them as a Json via the carregar fault function of PHP and send this data as a response to your Ajax request (something like json_encode ). The advantage of this is that Json will be interpreted by the browser and you can access it as an object, making it easier to manipulate.

Finally, your echo json_encode($dados); function will have the data needed to fill the page, simply generating the elements and filling them with the information that has just been returned.

Note that there are several tasks to complete. I have outlined what should happen so that you can get the data you want and present it to the customer. You should now study the technologies mentioned, and apply them to solving your problem.

    
20.02.2015 / 15:00