Quick answer:
Have a PHP page that posts to itself.
PHP/SQL performs the requireds check and the use an IF statement to include the JavaScript in the HTML output if the alert is to be given>
I usually use a variable called something like
$display_block or so and initialise it at the top of the page;
$display_block="";
Then add to it various bit of 'dynamically' generated html like so;
$display_block.="<td>Table cell data</td>";
The when you have closed your php tags:
?>
And start your html output, just include the $display_block variable where you need it:
<head></head>
<body>
<table>
<tr>
<? echo($display_block); ?>
</tr>
</body>
In your case you'd probably be putting the echo line in the <head> somewhere.
Hope this helps.
If I get time later, I'll come back and write a more full explaination but this should suffice.
🙂