User Name

This sample provides a simple demonstration of how you could handle the activation and removal of menu items based on some server side variable value, such as a variable that handles the user name of the logged in user.

Here's how the sample works:

Here's the code that does the magic:

<script language="javascript" type="text/javascript">
dmbAPI_Init();

// Since the DynAPI does not support items that have been disabled form within DHTML Menu Builder,
// we need to disable them here, using the API.

dmbAPI_setItemState(dmbAPI_getItemByCaption("UserName"), false);
dmbAPI_setItemState(dmbAPI_getItemByCaption("|"), false);

var n;
// Because we are going to be working with the UserName toolbar item it is a good idea
// to store its id in a variable so, every time we need to reference it we can just
// use the value in the variable.

var c = dmbAPI_getItemByCaption("UserName");

// So, let's see if the server-side variable $UserName contains any data
<? if(!isset($UserName) || ($UserName=="")) { ?>
  // The variable contained no data, which means that no one has logged in,
  // so we're going to disable the Utilities menu.

  n = dmbAPI_getItemByCaption("Utilities");
  dmbAPI_removeTBItem(n);
<? } ?>

// Let's check now if the user "root" has logged in
<? if($UserName != "root") { ?>
  // The user name variable does not match "root" so the root user is not logged in.
  //
In this case, we need to disable the "DHTML Menu Builder" menu.
  n = dmbAPI_getItemByCaption("DHTML Menu Builder");
  dmbAPI_setItemState(n, false);
  dmbAPI_setColor(n, "#C8C8C8");
<? } ?>

// Finally, let's display the current;y logged in user name
n = dmbAPI_getItemByCaption("UserName");
dmbAPI_setCaption(n, "<? echo (isset($UserName) && $UserName!="")?$UserName:"(not logged in)"; ?>");
</script>