How to set cookies

You use setcookie() like any other function, with parameters as described in the Codex.

How you would use it in a form would usually depend on how and when the $_COOKIE variable needs to be made accessible.

One common method, if you want a change in the DOM both to register immediately and to persist, is to use a Javascript/jQuery function whose effect is duplicated in the PHP functions that will draw upon $_COOKIE variables. The cookie will also typically be set or updated in the same script, commonly with the aid of jquery-cookie or js-cookie.