I have a custom module, in a javascript file that is located in the js folder of the module, I captured a value from a previously written function using an if statement. Ex: if($numb==6) show this img else show this img.

I need to access this value/var so I can display a particular image on my tpl.php when it loads.

How can I accomplish this in drupal7?


What you can do in this particular case is add the HTML for both the images inside tpl file. On the js file, you can display/hide the images according to your IF condition.

To capture JS value for use in tpl, you can use javascript/ajax to send the information through a url / xmlhttp and then use hook menu to grab the information inside module file, then send the variable to the tpl

  • Whoa, I was with u until the second half of that second paragraph. :) Let me do some more reading. Thank you for the suggestion. – A. Catch Sep 21 '15 at 18:30
  • Could u elaborate a bit on how to send url via js? Please road – A. Catch Sep 21 '15 at 20:21
  • Take a look at this jquery-ajax-post You have to add this function inside your drupal js file – Swarnendu Dutta Sep 22 '15 at 5:49
  • perfect, that worked - thank you for the help Swarnendu!!! – A. Catch Sep 24 '15 at 13:21

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