'Typo3 jQuery OnClick Events

i need some help for my fresh installed Typo3 Website.

I want to set new Values within my DataBase via jQuery onClick Event. So i guess i need to make use of this class: TYPO3\CMS\Core\Database\ConnectionPool. Can someone Guide me how i can make use of this class within JavaScript.

I already tried the following, but i can't get it to work

define(['jquery', 'TYPO3\CMS\Core\Database\ConnectionPool'], function($, conPool) {

   if(conPool.foo == 'bar'){
      conPool.init();
   }
});

Typo3 source

I appreciating any Help. Thanks



Solution 1:[1]

Your frontend should not be allowed to communicate directly with the database of your TYPO3 instance.
If you allow it anyone can inspect your javascript and get credentials to access your database - and do anything they like. :(

The correct way is a server request with the data you want to change.
then some PHP can compute the data (verify, adapt, ...) and change the value in the database (as PHP has the local credentials to access the database). Then it's up to you to give a notice in the front end if that operation was successful or not.
Such a server-request could be a complete page request, but a simple AJAX-call would be enough. Though you does not need to rebuild the complete page. From the Return of that AJAX request you can show only a notice about failure or success as a flash-message or similar.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bernd Wilke πφ