One way could be to call php script with AJAX. You can make php script just for setting cookie. I did it this way:
HTML:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<script>
function changeCookie(id)
{
data = 'id='+id;
$.ajax({
type: "POST",
url: 'test.php',
data: data,
dataType: 'html'
});
}
</script>
</body>
PHP:
<?php
$id = $_POST['id'];
setcookie("TestCookie", $id, time()+3600); /* expire in 1 hour */
?>
Whenever you call changeCookie function, it will set your cookie to value you supplied in function's argument. You can modify this script to send name and expiry date to PHP script.
Another way could be without PHP, just plan javascript. You can find more about that here.