将变量数据从JavaScript传递到Drupal/PHP是一件事。将信息从Drupal/PHP传递到JavaScript是另一件单独的事情。我想你在问题中指的是第一件事。所以我会继续回答。
从JavaScript传递变量到Drupal/PHP
1)实现菜单钩子。您可以在现有的贡献或新的自定义模块上执行此操作,这取决于您正在做什么。这很容易和直接,也在Drupal网站上有很好的文档记录。
$items = array();
$items['my_custom_callback/%'] = array(
'title' => 'My Custom Callback',
'description' => 'Listing of blogs.',
'page callback' => 'my_custom_php_function',
'page arguments' => array(1),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
让我们分解上面的例子。
$items['my_custom_callback/%']
$items只是一个数组,它可以被命名为任何你喜欢的名称。如果我的网站URL是www.alexanderallen.name,那么http://www.alexanderallen.name/my_custom_callback
就是我从JavaScript应用程序调用的资源。
在数组的键中,单词my_custom_callback后面的百分号是一个占位符。这是您将从JS传递数据到PHP的位置。因此,如果变量x的值为10,则从JQuery调用如下:
http://www.alexanderallen.name/my_custom_callback/10
当您加载该URL时,Drupal将查找您的菜单钩子,并调用您在“page callback”数组键中定义的函数。在这种情况下,那将是:
<?php
function my_custom_php_function($argument) {
echo $argument;
}
?>
在那里,您可以随意处理变量x的值,比如将其存储在数据库中。
如果您把菜单钩子放在自定义模块上,而您的模块名称为example_module,则菜单钩子和自定义PHP回调将如下所示:
<?php
function example_module_menu() {
$items = array();
$items['my_custom_callback/%'] = array(
'title' => 'My Custom Callback',
'description' => 'Listing of blogs.',
'page callback' => 'my_custom_php_function',
'page arguments' => array(1),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function my_custom_php_function($argument) {
echo $argument;
}
?>
如果你想从JQuery传递多个变量到Drupal/PHP,可以考虑在JavaScript中将它们转换为JSON,然后再传递到PHP。请记住,如果有效负载非常大,应该考虑使用JQuery的.post()而不是.get()。解码JSON的PHP函数在php.net上。
Don't forget to return $items at the end of the example_module_menu() function. If you wanted to pass two or more arguments from JavaScript to PHP then the menu item would look similar to this:
function example_module_menu() {
$items = array();
$items['my_custom_callback/%/%/%'] = array(
'title' => 'My Custom Callback',
'description' => 'Listing of blogs.',
'page callback' => 'my_custom_php_function',
'page arguments' => array(1, 2, 3),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function my_custom_php_function($arg1, $arg2, $arg3) {
$sum = $arg2 + $arg3;
echo "Hi". $arg1 .", X + Z = ". $sum;
}
?>
In this case I am passing three arguments to PHP. 'page arguments' specifies the part of the URL that you want to pass to PHP. If you were to write 'page arguments' => array(0),
, then the argument to your PHP function would be the string my_custom_callback.
2) Call the menu hook from your JS code using JQuery. JQuery provides various AJAX methods. Which one you use will depend on the format of the data your dealing with and it's size, amongst other things.
Most likely you will use one of two JQuery methods. .post() or .get(), bearing in mind that that post requests will allow for a bigger data payload than get requests. So if the size of variable x = 10, you might want to stick with .get(). Both .post() and .get() methods are an extension of, or rely on the .ajax() method. The .ajax() method is more flexible because it provides you with more options, like the ability to specify the format of the data (JSON or plain-text), send a username/password along with your request, choose between synchronous or asynchronous requests, and whether the browser should cache or not the request.
This is the method signature for the JQuery .get() method:
jQuery.get( url, [ data ], [ callback(data, textStatus, XMLHttpRequest) ], [ dataType ] )
If you were going to send your data using GET to Drupal/PHP, using the example above you could do something like this:
<script type='text/javascript'>
var name = 'alexander';
var x = 10;
var z = 20;
$.get(
// Callback URL.
"http://www.alexanderallen.name/my_custom_callback/"+ name +"/"+ x +"/"+ z
);
</script>
Note that the usage of the data argument on the .get() method is optional, and in my example I omitted it, but achieved the same effect by concatenating the data to pass to Drupal/PHP with the rest of the URL.
If I were to run that, the HTTP GET request would look like:
http://www.alexanderallen.name/my_custom_callback/alexander/10/20
and the response would look like:
Hi Alexander, X + Z = 30
Passing data from Drupal to JavaScript
For that then you would use Behaviors, I am not entering into details since I think that wasn't your question, but you can go to the official documentation for Drupal 6 behaviors here.