Why do people keep confusing php and javascript?
PHP is SERVER SIDE
JAVASCRIPT (like onclick) is CLIENT SIDE
You will have to use just javascript to redirect. Otherwise if you want PHP involved you can use an AJAX call to log a hit or whatever you like to send back a URL or additional detail.