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– NTT2016-08-16 14:10:32 +00:00Commented Aug 16, 2016 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??Panshul Khurana– Panshul Khurana2016-08-17 07:14:58 +00:00Commented Aug 17, 2016 at 7:14
2 Answers
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 ?Panshul Khurana– Panshul Khurana2016-08-17 09:03:44 +00:00Commented Aug 17, 2016 at 9:03
-
@PanshulKhurana Just updated. It is straightforward.Jimmy Ko– Jimmy Ko2016-08-17 09:15:11 +00:00Commented Aug 17, 2016 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 ??Panshul Khurana– Panshul Khurana2016-08-17 09:26:45 +00:00Commented Aug 17, 2016 at 9:26
-
@PanshulKhurana You only need to define
$items['mypage']. Thequery parameter is supported by default.Jimmy Ko– Jimmy Ko2016-08-17 09:35:47 +00:00Commented Aug 17, 2016 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 ;-)