从自定义 Drupal 表单访问 Drupal 的 $form_values 中的值
编辑:看来我的“数组爬行”技能还不够,谢谢你的建议。 此外,我发现我正在用普通的“=”而不是双“==”检查 $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'];
以及所有其他可能的组合,但可能我只是做错了。 实际上,我花了一个小时在网上爬行寻找答案,但我想出了任何东西。
EDIT: Seems like my "array-crawling" skills were not enough, thanks for the suggestions.
Moreover, I found out that I was checking the $discounttype condition with a plain "=" instead of a double "==". I guess banging your head on the same block of code for 3 hours makes you dumb and miss the most obvious errors.
First thing first, I'm on Drupal 6.
I have created a form with the following markup:
$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'),
);
The form renders correctly, as you can see from this picture: http://cl.ly/3D2C2h1t1m2B351L1T31
(the N and R values beside the € symbol are actually the value of the $discounttype variable, just for checking it)
Each white box is basically an istance of the beforementioned form.
I need to pass the value of the $discounttype variable on each submit, so I decided to set it as the title of the submit button.
My problem is that in the submit function itself I cannot access the value of the 'title' attribute contained in the #attributes array. Mainly because probably I don't know the right syntax.
So far I've tried
$foo = $form_values['attributes']['title'];
$foo = $form_values['#attributes']['title'];
$foo = $form_values['attributes']['#title'];
And every other possible combination, but probably I'm just doing it wrong.
It's actually an hour that I'm crawling the web searching for an asnwer but I came up with anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您应该提及表单元素 ID。
因此,您可以通过
$form_state["cart_".$index]['#attributes']['title'];
访问提交按钮但实际上,为什么不使用隐藏字段 ('#type' => 'hidden') ?
first, you should mention form element ID.
so, you can access submit button by
$form_state["cart_".$index]['#attributes']['title'];
but actually, why don't you use hidden field ('#type' => 'hidden') ?
我相信您必须使用
$form_state
而不是$form_values
。尝试一下:我建议在 Drupal 开发时使用 Devel 模块。它是开发过程中非常有用的工具,允许您查看页面加载时运行的所有查询、停止重定向以进行调试等等更多。
I believe you have to use
$form_state
instead of$form_values
. Give this a try:I recommend using the Devel module while developing for Drupal. It is an extremely helpful tool during development, allowing you to see all the queries run when a page loads, stop a redirect to debug, and much more.