The sample you posted does nothing useful; it simply replaces the "<?php myFunction(); ?>" string in your sample with "Works"... Simply add an onclick event to a button but don't expect that clicking the button will call your php fuction... In your sample the function will be called when the page loads...