为什么我的 drupal 表单提交函数没有被调用?

发布于 2024-10-21 17:32:20 字数 1497 浏览 1 评论 0原文

呃,这可能很简单,但它让我发疯。我有一个简单的表单(只是一个提交按钮),我使用 hook_nodeapi() 将其插入到节点中。它被正确插入,并且当我提交时页面刷新,但它永远不会点击 form_submit() 函数。这是代码:

function fantasy_stocks_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  $form = drupal_get_form('fantasy_stocks_buy_me_form', $node);
  switch ($op) {
    case 'view':
      $node->content['body']['#value'] .= $form;
      break;
  }
}

function fantasy_stocks_buy_me_form(&$form_state, $node) {
  $form['submit'] = array(
    '#type' => 'submit',
    '#title' => t('Buy') . ' ' . $node->title,
    '#description' => t('Add') . ' ' . $node->title . ' ' . t('to your stock portfolio.'),
    '#value' => t('Buy') . ' ' . $node->title,
    '#submit' => TRUE
  );
  $form['node_added'] = array(
    '#type' => 'hidden',
    '#value' => $node->nid
  );
  $form['#submit'][] = 'fantasy_stocks_buy_me_form_submit';
  return $form;
}


function fantasy_stocks_buy_me_form_submit( $form, &$form_state ) {
  $message = 'You have submitted the ' . $form_id . ' form which contains the following data:<pre>' . print_r($form_values,true) . '</pre>';
  drupal_set_message(t($message));
}

我尝试在提交函数中添加 echo 和 die() ,但它肯定不会被调用。我还尝试省略 $form['#submit'] 声明,因为默认值应该处理它,但无济于事。我知道我一定错过了一些愚蠢的事情。有什么想法吗?

另外,一件看起来很奇怪的事情是,表单使用以下标签进行渲染:

<form action="/MLMBid/node/5"  accept-charset="UTF-8" method="post" id="fantasy-stocks-buy-me-form-1"> 

将“-1”附加到表单 ID 后正常吗?

Ugh, this is probably something simple, but it is driving me crazy. I've got a simple form (just a submit button) that I am inserting into a node using hook_nodeapi(). It gets inserted correctly, and the page refreshes when I submit, but it is never hitting the form_submit() function. Here's the code:

function fantasy_stocks_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  $form = drupal_get_form('fantasy_stocks_buy_me_form', $node);
  switch ($op) {
    case 'view':
      $node->content['body']['#value'] .= $form;
      break;
  }
}

function fantasy_stocks_buy_me_form(&$form_state, $node) {
  $form['submit'] = array(
    '#type' => 'submit',
    '#title' => t('Buy') . ' ' . $node->title,
    '#description' => t('Add') . ' ' . $node->title . ' ' . t('to your stock portfolio.'),
    '#value' => t('Buy') . ' ' . $node->title,
    '#submit' => TRUE
  );
  $form['node_added'] = array(
    '#type' => 'hidden',
    '#value' => $node->nid
  );
  $form['#submit'][] = 'fantasy_stocks_buy_me_form_submit';
  return $form;
}


function fantasy_stocks_buy_me_form_submit( $form, &$form_state ) {
  $message = 'You have submitted the ' . $form_id . ' form which contains the following data:<pre>' . print_r($form_values,true) . '</pre>';
  drupal_set_message(t($message));
}

I've tried adding an echo and die() in the submit function, it is definitely not getting called. I've also tried leaving off the $form['#submit'] declaration, as the default should take care of it, but to no avail. I know I must be missing something stupid. Any ideas?

Also, one thing that seemed weird is that the form gets rendered with the following tag:

<form action="/MLMBid/node/5"  accept-charset="UTF-8" method="post" id="fantasy-stocks-buy-me-form-1"> 

Is that normal, to have the "-1" appended to the form id?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路弥 2024-10-28 17:32:20

终于想通了。创建提交按钮的部分:

$form['submit'] = array(
    '#type' => 'submit',
    '#title' => t('Buy') . ' ' . $node->title,
    '#description' => t('Add') . ' ' . $node->title . ' ' . t('to your stock portfolio.'),
    '#value' => t('Buy') . ' ' . $node->title,
    '#submit' => TRUE
  );

我从教程中复制了部分内容,显然该行

'#submit' => TRUE

不应该在那里。它覆盖了表单提交处理程序,这使得 drupal 尝试查找名为 TRUE 的函数。知道这是愚蠢的事情。

Finally figured it out. The part that was creating the submit button:

$form['submit'] = array(
    '#type' => 'submit',
    '#title' => t('Buy') . ' ' . $node->title,
    '#description' => t('Add') . ' ' . $node->title . ' ' . t('to your stock portfolio.'),
    '#value' => t('Buy') . ' ' . $node->title,
    '#submit' => TRUE
  );

I copied parts of this from a tutorial, and apparently the line

'#submit' => TRUE

should not be there. It was overriding the form submit handler, which made drupal attempt to look for a function called TRUE. Knew it was something stupid.

尽揽少女心 2024-10-28 17:32:20

我倾向于认为表单 id 上的“-1”是问题的根源。然而,不仅仅是“-1”,为什么表单 id 用“-”而不是“_”呈现,就像在代码的其余部分中引用的那样。解决这个问题,你的问题就应该得到解决。

不幸的是,我还没有使用过 Drupal(只是 Joomla)。我会尝试更改代码以匹配表单 ID 呈现为 (fantasy-stock-buy-me-form-1) 而不是您当前拥有的内容。

I would tend to think that the "-1" on the form id is the root of your problem. However, not just the "-1" why is the form id being rendered with "-" instead of "_" like is being referenced in the rest of the code. Solve that and your problem should be fixed.

Unfortunately, I haven't used Drupal yet (just Joomla). I would try changing the code to match what the form id is being rendered as (fantasy-stock-buy-me-form-1) instead of what you currently have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文