我试图将参数传递给一个由" 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;
}