I think this question comes up somewhat often, and the comment answers are correct; but you also have to understand what's happening during the request. I'll try to keep it kind of simple.
On the SERVER, PHP processes all the PHP tags of your file, and turns it into a file consisting of pure HTML, Javascript, and CSS. The server sends this to the CLIENT (the person with the web browser hundreds of miles away). That client doesn't even know what PHP is, and the server will actually then shut down that little PHP environment it had created; all variables are destroyed, and it waits for the next client request to process a page again.
Using a specially-coded in-page request, often referred to as AJAX (Asynchronous Javascript and XML, the XML part being a misnomer) you can start new requests against the PHP server to save information, request new information, etc, without loading a new webpage.
HOWEVER, your specific question has a unique aspect to it; you want to set a cookie. Although you normally only see cookies when working in PHP, the cookies are actually stored on the client's computer, and are sent to the server on each request. Javascript is able to access them on its own, and some libraries can help with that.
I haven't followed this tutorial myself, but a quick Bing search found this, which might help you: http://www.electrictoolbox.com/jquery-cookies/