在同一页面上的PHP变量中获取HTML代码

I am having a problem in using ob_get_clean function.I am putting a simplified version of the problem here. The html body is changed a little bit using javascript first and then mailed. Here is the file containing php and html code.

<?php
    ob_start();
?>
<h1 id="check" style="color:blue;">This is first comment</h1>
<script>
    document.getElementById("check").innerHTML = "This is second comment";
</script>
<?php
    $test = ob_get_clean();
    echo $test;
    $to = "abc@xyz.com";
    $subject = "Subject";
    $headers = "From: Sender
";
    $headers .= "Content-Type: text/html
";
    $message="";
    $message .= <<< EOF
    $test

    EOF;
    mail($to,$subject,$message,$headers);
?>

In the line echo $test, the html output of the page is

This is second comment

However the email received contains

This is first comment

I want that the email that is sent should be "This is second comment" that is the html code that has been changed using javascript. Any help would be appreciated.

No, the php part is done on the server.

The JS is done on the client side long after the php execution has ended.

Maybe you should not mix script and php like this. Better to use an ajax call.

It looks like the php script is called before your script changed your text.