I need to update a span element with a value from an update PHP function (that performs mySQL queries), triggered after a button click. I used jQuery/Ajax to achieve this, via a function in another file.
Apparently PHP logs in this case require the following form:
echo '<script> console.log("My message"); </script>';
So I put these in my PHP function file, which looks like the following:
if(isset($_POST['myAction'])) {
    if ($_POST['myAction'] == "right_action") {
        echo '<script> console.log("ENTERED"); </script>';
        update(...)
        ...
    }
}
function init()
{       
    ...
    echo '<script> console.log("Successfully connected to database"); </script>';
    return ...;
}
function fetch(...) {
    init();
    ...
    echo '<script> console.log("Database fetch successful"); </script>';
    echo '<script> console.log(' . result . '); </script>'; // equals 49
    return ...;
}
function update(...) {  
    init();
    ... 
    echo '<script> console.log("Database update successful"); </script>';   
    return fetch(...);
}
On the one hand, the logs appear properly while the PHP is executing.
However it seems that the return value of the update PHP function, from the perspective of the Ajax function, contains all the logs. Here is the Ajax function:
$.ajax({
            method: "POST",
            url: "my_php_file.php",
            data: { myAction: 'myAction',
                    (params) },
            success: function(result) {
                console.log("Success at Ajax transfer-to-PHP");
                console.log("RESULT: " + result);
                spanElement.load(result);       
            },
            error: function(error) {
                console.log("Error at Ajax transfer-to-PHP: " + error);
            }
        });
And the Ajax log:
RESULT
<script> console.log("ENTERED"); </script><script> console.log("Successfully connected to database"); </script><script> console.log("Database update successful"); </script><script> console.log("Successfully connected to database"); </script><script> console.log("Database fetch successful"); </script><script> console.log(49); </script>
The same behavior happens when I print in Ajax the content of the span element, initially filled with the result from the fetch PHP function. It prints all the logs; but then it does print the value (while the update mechanic above doesn't even).
 
    