Comment
Forewards : <? ... ?> is PHP code
/* These are comment lines which are not executed */
To view the selected color you need to make a second visit , or reload the page.
When php parser queries the server this latter returns the requested page , so the bgColor instruction , that the stored file has , is returned and afterwards the server registers the new instruction for bgColor that the cookie contains into the client's machine ( as a Cookie ), so that on next request this instruction will be executed.
No matter the color we select and send as bgColor , the JavaScript code at side , will affect the script till the window's alert ( ) , as soon as we close this alert ( ) the bgColor will be modified in red anyway !
This means that JavaScript code is executed after PHP's output to the Client and if we pick up the form value at the JavaScript line location , we will be able to write straight away the desired Cookie value for bgColor without reloading !

Let's do the form's value pick up ... bgColor.php
<?
  $bgcolor=$HTTP_COOKIE_VARS['bgcolor'];
  if ($HTTP_POST_VARS['bgcolor'])
  { setcookie('bgcolor',$HTTP_POST_VARS['bgcolor'],
  time()+(60*60*24*7)); }

  /* $bgcolor = empty($bgcolor) ? 'yellow' : $bgcolor;
↑     Equivalent alternative Syntax     ↓     */
if(empty($bgcolor)) { $bgcolor='yellow'; };
?>
<html><body bgcolor="<? echo $bgcolor; ?>">
<form action="<?= $PHP_SELF ?>" method="POST">
  <select name="bgcolor">
        <option value=""></option>
        <option value="red">Red</option>
        <option value="green">Green</option>
        <option value="blue">Blue</option>
  </select>
<input type="submit"></form>
<Script language=javascript>
alert ( document.bgColor+'...text' );
document.bgColor="red";
</Script>
</body></html>