沃梦达 / 编程问答 / php问题 / 正文

如何将数组传递给 Drupal 菜单回调

How to Pass Arrays to Drupal Menu Callbacks(如何将数组传递给 Drupal 菜单回调)

本文介绍了如何将数组传递给 Drupal 菜单回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JavaScript 中有两个数组:var xcoord = [];var ycoord = [];经过一些处理,每个数组包含 15 个数值.我想将此数据发送到 Drupal 中的菜单回调.

I have two arrays in JavaScript: var xcoord = []; and var ycoord = []; After some processing, each array contains 15 numeric values. I'd like to send this data to a menu callback in Drupal.

我的 AJAX 代码:

My AJAX code:

$.post('http://mysite.com/?q=menu_example/my_page', 
    {'ycoord[]': ycoord, 'xcoord[]': xcoord }
);

Drupal PHP:

Drupal PHP:

$items['menu_example/my_page/%/%'] = array(
    'title' => 'My Page', 
    'description' => 'i hope this works', 
    'page callback' => '_graphael', 
    'page arguments' => array(2, 3), // fill this, 
    'access callback' => true,
    'type' => MENU_CALLBACK,
);

在我的控制台中,我看到 xcoordycoord 的值正在正确传输,但回调并不完全正常.我的问题:如何将数组传递给菜单回调?我还应该在 $items 键中使用 % 占位符吗?

In my console, I see that the values for xcoord and ycoord are being properly transmitted, but the callback isn't quite working. My question: how would I pass arrays to a menu callback? Should I still use a % placeholder in the $items key ?

推荐答案

您传递给 $.post() 的第二个参数未附加到您作为第一个参数传递的 URL;它是在 $_POST 中传递给 PHP 的数据.

The second argument you pass to $.post() is not attached to the URL you pass as first argument; it is data that is passed to PHP in $_POST.

菜单回调的正确定义应如下所示.

The correct definition for the menu callback should be the following one.

$items['menu_example/my_page'] = array(
  'title' => 'My Page', 
  'description' => 'I hope this works', 
  'page callback' => '_graphael', 
  'access callback' => TRUE,
  'type' => MENU_CALLBACK,
);

您的页面回调应该会在 $_POST 中找到从 jQuery 传递的数据.

Your page callback should then find the data passed from jQuery in $_POST.

这篇关于如何将数组传递给 Drupal 菜单回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何将数组传递给 Drupal 菜单回调

基础教程推荐