从自定义Drupal表单访问Drupal的$ form_values中的值

时间:2011-06-14 16:19:53

标签: drupal drupal-6 drupal-forms

编辑:似乎我的“阵列爬行”技能还不够,谢谢你的建议。 而且,我发现我用普通的“=”而不是双“==”来检查$ discounttype条件。我想在同一块代码上敲打你的头3小时让你愚蠢而错过最明显的错误。

首先,我在Drupal 6上。

我创建了一个带有以下标记的表单:

$form["cart_".$index] = array(
'#type' => 'image_button',
'#src'=> 'files/imghome/sidebar-add-demo.gif',
'#attributes' => array('rel' => '#item', 'class' => 'buybutton', 'title' => $discounttype),
'#prefix'=>'<p class="renewprop">'.$newren.' for '.$node_abb->field_tipo_abb_value.':</p><p class="renewblock"><span class="pricetag">'.$node_abb->field_prezzo_value.''.$discounttype.'</span>',
'#suffix' =>'</p>' ,
'#submit' =>array('usercp_form_submit'),
);

表单呈现正确,如下图所示:http://cl.ly/3D2C2h1t1m2B351L1T31 (€符号旁边的N和R值实际上是$ discounttype变量的值,仅用于检查它)

每个白框基本上都是前面提到的形式。

我需要在每次提交时传递$ discounttype变量的值,所以我决定将其设置为提交按钮的标题。

我的问题是,在提交函数本身,我无法访问#attributes数组中包含的'title'属性的值。主要是因为我可能不知道正确的语法。

到目前为止,我已经尝试了

$foo = $form_values['attributes']['title'];
$foo = $form_values['#attributes']['title'];
$foo = $form_values['attributes']['#title'];

以及其他所有可能的组合,但可能我只是做错了。 实际上是一个小时,我正在爬网搜索asnwer,但我想出了什么。

2 个答案:

答案 0 :(得分:0)

我认为您必须使用$form_state而不是$form_values。试一试:

$foo = $form_state['clicked_button']['#attributes']['title'];

我建议在为Drupal开发时使用Devel module。它在开发过程中是一个非常有用的工具,允许您查看页面加载时运行的所有查询,停止重定向调试,以及更多

答案 1 :(得分:0)

首先,您应该提及表单元素ID。 所以,您可以通过$form_state["cart_".$index]['#attributes']['title'];访问提交按钮 但实际上,为什么不使用隐藏字段('#type'=&gt;'隐藏')?