I need to create a link, which when clicked should be able to set a session variable.

I have used l() function to create the link and tried this but this is not working.

'name' => l(t($n['name']),'edit_details',array('attributes' => array('onclick' =>$_SESSION['id']=$n['id'] ))),
  • Have you checked if the onClick attribute is being applied in the markup? This seems like an issue with your JavaScript being incorrect rather than Drupal not applying the attribute. Commented Oct 3, 2016 at 12:32

3 Answers 3


Links do not allow any callback action, you can not set a variable on click event.

Instead you can either make it a custom form, where submit button is themed to look like a link instead of link and on form submission set the value in session.

Or other way would be, if its not secured information, you can pass it as query parameter in URL.

  • i tried passing the value in a url, it is not working due to the url encoding.
    – ana
    Commented Oct 3, 2016 at 6:26

You can do this modification: 'name' => l(t($n['name']),'edit_details',array('query' => array('new_id' => $n['id']))),

And then, in your edit_details page PHP code you can add this:

$_SESSION['id'] = $_GET['new_id'];

If you don't have a module with PHP code for this page you can add a block, use the PHP filter and add the $_SESSION logic there.


One easy way could be : Implement hook_menu to provide a link to a function Then in function you can use any code you desire

something like this :

function yourmdoule_menu()
  $items['test/%'] = array(
    'title' => 'Some Function',
    'description' => 'Some function will be called by calling it',
    'page callback' => 'my_function',
    'type' => MENU_CALLBACK,
    'access callback' => true,
    'page arguments' => array(1),
  return $items;

and then :

function my_function($id = 1){
  /// run every want you want

so now you can add a button with link to 'test/$nid' and it will call your desired function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.