我如何将javascript变量值传递给php

I want the JavaScript variable res value in a PHP variable. I have tried sending the value into a hidden field but it does not work. How can i get the value so that it can be used in PHP?

<script type="text/javascript">
    (function () {
        setTimeout(function () {
            H5P.externalDispatcher.on('xAPI', function (event) {
                if (event.data.statement.result.response == undefined && event.data.statement.result.completion == true) {
                    alert("Topic has been completed");
                    res = 1;
                }
            });
        }, 1000)
    })();
</script>

You can't.

JavaScript and PHP are totally different things. JavaScript executes on Client side and PHP gets executed on server side. So when PHP is getting executed it doesn't even know anything about JavaScript.

Workaround

Use AJAX requests to send Client/JavaScript data to Server/PHP for processing.