I am currently working on a project where i am required to pass Javascript variable value to php, i am able to do it vice-versa but unable to accomplish, this one. Is there any way to get this done in Drupal 7 ?

  • You can refer to this link that already clearly explained.
    – NTT
    Aug 16 '16 at 14:10
  • I looked into the link above, and figured out that whatever data you will pass in the URL will be displayed as output of the call back function. Rather i would like to make it dynamic, a variable which recieves a value is passed to a php page . Any suggestions on how to make it dynamic?? Aug 17 '16 at 7:14

If you want to pass the value during loading page. You can do it by appending the JS value as query variable in path (e.g. http://www.example.com/new/page?demo=true) for requesting the next page. In PHP, you can use API drupal_get_query_parameters to read the data.

// retrieve data from www.example.com?data=value
$query = drupal_get_query_parameters();
$value = $query['data'];
  • Can you please provide a small example using drupal_get_query_parameters ? Aug 17 '16 at 9:03
  • @PanshulKhurana Just updated. It is straightforward.
    – Jimmy Ko
    Aug 17 '16 at 9:15
  • in my hook_menu the URL pattern that i have mentioned is as follows : $items['mypage?%'] is this the correct way to mention the URL ?? Aug 17 '16 at 9:26
  • @PanshulKhurana You only need to define $items['mypage']. Thequery parameter is supported by default.
    – Jimmy Ko
    Aug 17 '16 at 9:35

You can do it via a AJAX call. First you need to create a custom module, with a hook_menu() implementation. Then you can call that path via AJAX and process the value with PHP. Don't forget to filter the user input ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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