Code working normally in a fresh HTML file but not in a Drupal block
Inputing PHP or Javascript through the UI is almost always a bad idea, and can happen to be impossible in some cases, for security reasons. 

I recommend another approach, for example creating a block in a custom module with the various block hooks.

This way, you won't bypass Drupal security and you'll have a much more versatile and robust solution.