Since what you're talking about is an interaction between client-side and server-side code, you'll have to have some way to transport data from one to the other (either uni- or bi-directionally, depending upon your needs).
One common way to do this is via AJAX.