我正在尝试将参数传递给由“durpal_get_form”调用的表单,该函数是从 JS 脚本在 AJAX 中调用的 PHP 函数。
所以我做了这样的事情:
// Load form
$form = drupal_get_form("_bto_ajax_submenu_organize_form", array(
'territories' => $territories,
'accommodations' => $accommodations,
'activities' => $activities,
'language' => $language,
));
事实是我的表单被构建了两次。第一次我有我的参数值,但第二次它们都等于“NULL”......所以在我的表单提交函数中,我无法取回它们。我试图将它们放在 $form_storage 中的隐藏字段中……没有任何效果。
有任何想法吗 ?
编辑:这是我的代码。有些部分你可能不明白,但至少你会对问题有一个全局的认识。
表格和提交:
/**
* Organize form
*/
function _bto_ajax_submenu_organize_form($form, &$form_state, $args)
{
$form['place'] = array(
'#prefix' => '<div class="left">',
'#title' => t('Where do you want to go ?', array(), array('langcode' => $args['language'])),
'#type' => 'select',
'#options' => _bto_ajax_submenu_organize_form_get_territories($args['territories'], $args['language']),
);
$form['activity'] = array(
'#title' => t('What do you want to do ?', array(), array('langcode' => $args['language'])),
'#type' => 'select',
'#options' => _bto_ajax_submenu_organize_form_get_activities($args['activities'], $args['language']),
'#suffix' => '</div>'
);
$form['accommodation'] = array(
'#prefix' => '<div class="right">',
'#title' => t('Where do you want to lodge ?', array(), array('langcode' => $args['language'])),
'#type' => 'select',
'#options' => _bto_ajax_submenu_organize_form_get_accommodations($args['accommodations'], $args['language']),
'#suffix' => '</div>'
);
if ($form_state['build_info']['args'][0]['language'] != NULL) {
switch ($args['language']) {
case 'fr':
$form['test'] = array(
'#type' => 'hidden',
'#value' => 'fr',
);
break;
case 'en':
$form['test'] = array(
'#type' => 'hidden',
'#value' => 'en',
);
break;
case 'de':
$form['test'] = array(
'#type' => 'hidden',
'#value' => 'de',
);
break;
default:
$form['test'] = array(
'#type' => 'hidden',
'#value' => 'fr',
);
break;
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t("Let's go !", array(), array('langcode' => $args['language'])),
);
return $form;
}
/**
* Organize form submit
*/
function _bto_ajax_submenu_organize_form_submit($form, &$form_state)
{
dsm($form_state['build_info']['args']);
$values = $form_state['values'];
$storage = $form_state['storage'];
$query = array();
dsm($values);
// Territory
if ($values['place'] != 0)
$query['place'] = $values['place'];
// Activity
if ($values['activity'] != 0)
$query['filter'] = $values['activity'];
// Hébergements
if ($values['accommodation'] != 0)
$query['filter2'] = $values['accommodation'];
$url = get_node_path('109', $storage['lang']);
$url = str_replace('fr/', '', $url);
$url = str_replace('en/', '', $url);
$url = str_replace('de/', '', $url);
drupal_goto($url, array('query' => $query));
}
挂钩菜单:
/**
* Hook menu
* @return array $items Contient les items du menu
*/
function bto_menu()
{
$items = array();
$items['ajax/%'] = array(
'access callback' => true,
'page arguments' => array(1),
'page callback' => '_bto_ajax',
'type' => MENU_CALLBACK,
);
//...
return $items;
}
以及形式的调用:
/**
* Callback ajax route
* @param $request
*/
function _bto_ajax($request)
{
extract($_POST);
switch ($request) {
// ...
case 'menu-principal-organiser':
$output = _bto_ajax_submenu_load_organize($vars);
break;
}
print $output;
}
/**
* Organize your trip
*/
function _bto_ajax_submenu_load_organize($vars)
{
$vars = json_decode($vars);
$territories = $vars->territories;
$accommodations = $vars->accommodations;
$activities = $vars->activities;
$language = $vars->language;
// Load form
$form = drupal_get_form("_bto_ajax_submenu_organize_form", array(
'territories' => $territories,
'accommodations' => $accommodations,
'activities' => $activities,
'language' => $language,
));
// Load packaged offers
// ...
// Load template
$output = theme('submenu-organize', array(
'form' => drupal_render($form),
'packaged_offers' => $packaged_offers,
'language' => $language,
));
return $output;
}