W
W
Web Building2021-01-22 22:39:04
PHP
Web Building, 2021-01-22 22:39:04

The result of the Like / DisLike buttons are displayed on the page only after it is updated and the database page is updated. Why??

Hi all! The question is in the title. That is, in order for the last summary result to appear, you need to update the database page (and then I will see a new result in the database) and the page of the like button itself (and the last summary result will appear here: +1 to the previous one, i.e. each click on the button) . After all, the result should appear without reloading the site page and the database page. Help, who can?! :)

Here is an example for the button: Like.

index.php

// Click counter saved in the database

require_once('counter.php');    // обработчик кнопок Like / DisLike

$dsn = "mysql:host=localhost;port=3306;dbname=название БД;charset=utf8";
$pdo = new PDO($dsn, 'имя пользователя БД', 'password', $опционально);

  $statement = $pdo->query("SELECT `value` FROM counterLike WHERE `idLike` = 1");
  $like = (int)$statement->fetchColumn();


Further, the HTML form ................. is not essential, because its not the problem.

main.js

// Клик по кнопке Like
    $(document).ready(function() {

        var linklike = $("#btnLike"),
            counterlike = $("#like_count"),
            countlike = 0;

        // Блок - Лайк ( Like )
        linklike.on('click', function(e) {
            e.preventDefault();

            var ajax = $.ajax({
                method: 'post',
                url: 'https://site.php/counter.php',
                dataType: 'text',
                data: {
                    'countlike': 1
                }
            });
            ajax.done(function(data) {
                data = JSON.parse(data);
                counterlike.html(parseInt(data));
            });
        });
    });


counter.php

declare(strict_types=1);
  error_reporting(E_ALL);

  // конфигурация БД
$servername = 'localhost';
$dbname = 'название БД';   
$database = 'имя пользователя БД';   
$password = 'password';    

$dsn = "mysql:host=localhost;port=3306;dbname=название БД;charset=utf8"; 
  
try {  
      $pdo = new PDO($dsn, 'имя пользователя БД', 'password', $опционально);
        // Для таблице counterLike
    if (isset($_POST['countlike']) && (int)$_POST['countlike'] === 1) {
        $stmt = $pdo->query("SELECT `value` FROM counterLike WHERE `idLike` = 1");  // Для Like
        $like = (int)$stmt->fetchColumn();
        $like++;
   $paramsLike = [
     ':id' => 1,
     ':value' => $like  
   ];
   $query = "UPDATE `counterLike` SET `value` = :value WHERE `idLike` = :id";
   $stmt = $pdo->prepare($query);
   $stmt->execute($paramsLike); 
     echo json_encode($like);  
    }

} catch (Exception $e) {
  echo $e->getMessage();
 }


And here is the final result of the "counterLike" SQL table in the DB:
600b3b04e915f555251387.png

SoS! Help!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question