There’s two ways to do this: (1) a session, and (2) a form.
A session is used to store values between page requests – it uses a cookie to identify your browser, which PHP manages for you. Your category could be set into a variable, and then re-read at page load.
The other approach is to wrap both your category and sub-category controls in a form. Clicking submit will send both of these values to the server. When you are setting just the category, you’ll not have a sub-category; the server should reply with the same category and a menu for subcategories. If you re-render the category menu (even though it is already set), resubmitting the form will ensure the category is not lost.
Out of the two of these, I’d probably do the second one.
solved How to make a variable retain its value after reloading, in PHP?