How can a JavaScript onclick event value be passed to a PHP variable for database manipulation?

To pass a JavaScript onclick event value to a PHP variable for database manipulation, you can use AJAX to send the value to a PHP script that will handle the database operations. In the JavaScript onclick event, you can make an AJAX request to a PHP script with the value as a parameter. In the PHP script, you can retrieve the value from the request and perform the necessary database manipulation.

<?php
// PHP script to handle AJAX request and database manipulation

if(isset($_POST['value'])) {
    $value = $_POST['value'];

    // Perform database manipulation with the value

    // Example: Insert the value into a database table
    $conn = new mysqli('localhost', 'username', 'password', 'database');
    $stmt = $conn->prepare("INSERT INTO table_name (column_name) VALUES (?)");
    $stmt->bind_param("s", $value);
    $stmt->execute();

    // Close database connection
    $stmt->close();
    $conn->close();

    echo "Value inserted into database successfully!";
} else {
    echo "Error: Value not received.";
}
?>