3

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'] ))),
1
  • 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. Oct 3, 2016 at 12:32

3 Answers 3

0

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.

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

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.

0

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 that you have read and understand our privacy policy and code of conduct.

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