How can PHP be utilized to dynamically update a counter when a specific element, such as a word, is clicked?

To dynamically update a counter when a specific element is clicked, you can use PHP in combination with JavaScript. When the element is clicked, a JavaScript function can be triggered to send an AJAX request to a PHP script that increments the counter in a database. The updated count can then be retrieved and displayed on the page without needing to refresh.

<?php
// PHP script to handle incrementing the counter in the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "counter_db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Increment the counter in the database
$sql = "UPDATE counter_table SET count = count + 1 WHERE id = 1";
$conn->query($sql);

// Close connection
$conn->close();
?>