Welcome to php
OK the most important thing that you need to understand is that PHP is a SERVER SIDE scripting language.
So that means the php script has to be sent to the server (usually by pressing a submit button). When the php script is run on the on the server the code you have in your php script in run.
What you are trying to do requires CLIENT SIDE scripting. In essenece javascript is the scripting language you'd need to use.
HTH
GM