如何将jQuery变量传递给Drupal(ajax)

7

我想在不刷新页面的情况下将var x = 10;传递给Drupal/PHP变量$x。

有人能提供建议吗?


非常感谢您的回复。 实际上,我正在为电子商务网站(Drupal 6)开发自定义模块。 在产品页面上,我有4个属性作为文本字段(宽度英寸、宽度英尺、高度英寸、高度英尺)。通过做一些计算,我得到了该产品的新价格。我使用jQuery进行了计算。 现在,我修改了“uc_add_to_cart_form”,添加了1个隐藏字段,并使用$_SESSION将jquery新价格传递到该隐藏字段中。到这一步为止,一切都正常。

我想将这个新价格保存到产品节点中。但是我不能将这个新价格设置为$node->price。 我正在使用uc_ajax_cart来实现“加入购物车”按钮。

有人能否请建议我如何继续进行?


6
这个问题不是很有意义,请详细说明你想要做什么。简而言之,除非你在对$var进行某些操作,否则给$var分配一个值是没有意义的。请求完成后,变量和其他所有内容都将消失。 - Berdir
2个回答

35

将变量数据从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 that gets called from JQuery asynchronously. 
*/
function my_custom_php_function($argument) {
  // Do something with $argument...
  echo $argument;
}
?>

在那里,您可以随意处理变量x的值,比如将其存储在数据库中。

如果您把菜单钩子放在自定义模块上,而您的模块名称为example_module,则菜单钩子和自定义PHP回调将如下所示:

<?php
/**
* @file example_module.module Handles fancy AJAX functionality.
* 
*/

/**
* Implementation of hook_menu
*
* @see http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_menu/6
*/
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 that gets called from JQuery asynchronously. 
*/
function my_custom_php_function($argument) {
  // Do something with $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) {
  // Do something ...
  $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.


4

你需要知道:

菜单API:
http://api.drupal.org/api/drupal/includes--menu.inc/group/menu/7
一些Drupal JSON函数(可能)
http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_json_encode/7
variable_set()
http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/variable_set/7
以及jQuery ajax API:
http://api.jquery.com/category/ajax/

基本上,你需要创建一个委派到回调的菜单项(要小心权限),然后使用$.ajax()调用它。如果你需要对响应进行处理,请参见json_encode功能。

您可能想查看这里的一些菜单示例:
http://drupal.org/project/examples

我认为有一个Drupal接口可以使用jQuery ajax API。如果有更好的方法,或许有人可以评论一下。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接