简体   繁体   中英

Why isn't my drupal form submit function being called?

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM