Perform SQL query from Javascript

3

Good morning,

I understand that you can not perform an SQL query from Javascript, but I do not know if the information I have found through Google may be obsolete or not. The fact is that I have some elements that when pressed, a small script is executed:

var heart = 0;
$('#heart').click(function(){
    console.log(heart);

    if (heart == 0) {
        document.getElementById('heart').style.color = '#e74c3c ';
        document.getElementById('heart').style.background = 'transparent';
        heart = 1;
    } else {
        document.getElementById('heart').style.color = 'white';
        document.getElementById('heart').style.background = '#5dc21e';
        heart = 0;
    }
});

What I would like is that I not only change the styles, I also insert some data in a table in the database or remove them.

With onclick I understand that I can not do what I want either because it would also take me to a script.

I can not think of how to solve the problem.

I would appreciate any help.

    
asked by JetLagFox 25.03.2017 в 11:18
source

1 answer

1

In local there is no problem using the API that I indicated in the comment (if the browser supports it, of course).

In remote ... you have no choice but to use code on the server: PHP, Nodejs, ... whatever you prefer. And some Javascript in the client. Javascript, by itself, can not establish connections with BBDD ; is limited to HTTP requests, and, lately, to WebSockets connections.

The trick consists of making AJAX calls to the WEB server, and that it responds with the data you want. There is a lot of documentation on AJAX on this site and on the Internet.

If the final version of your code is going to need an external DB server, my recommendation is that you start using it from now . The code between using IndexedDB and making AJAX calls is not directly transportable , and you can take little advantage of one for the other (checking data and little else). In addition, AJAX requests, on the server side, will show you some additional inconvenient . You'll already be asking; -)

    
answered by 25.03.2017 / 11:47
source